【问题标题】:XSLT: Xpath to count items with attribute value matching another specific siblingXSLT:Xpath 对属性值与另一个特定同级匹配的项目进行计数
【发布时间】:2017-03-31 09:18:03
【问题描述】:

我需要通过 XSLT 处理以下(简化的;还有更多属性行)XML 文件:

<Collection Name="Columns" Type="OutputColumn">
<SubRecord>
    <Property Name="Name">SID</Property>
    <Property Name="SqlType">BigInt</Property>
    <Property Name="Derivation">inputstream1.SID</Property>
    <Property Name="Description">Main key</Property>
</SubRecord>
<SubRecord>
    <Property Name="Name">name</Property>
    <Property Name="SqlType">Char</Property>
    <Property Name="Derivation">inputstream2.name</Property>
    <Property Name="Description">Surname</Property>
</SubRecord>
    <SubRecord>
    <Property Name="Name">street</Property>
    <Property Name="SqlType">Char</Property>
    <Property Name="Derivation">inputstream1.streetname + inputstream1.number</Property>
    <Property Name="Description">Full street</Property>
</SubRecord></Collection>

我现在需要知道两件事作为 XSLT 中的变量来决定在 if-Condition 中进一步做什么:

  1. 是否存在至少一个 SubRecord,其中 substring-after(Derivation, '.') 与兄弟属性“Name”匹配(就像在前两个 SubRecord 中一样)?
  2. 是否至少有一个 SubRecord 的 substring-after(Derivation, '.') 与兄弟属性“名称”不匹配(就像在第三个 SubRecord 中一样)?

我尝试通过 Xpath 使用 Count()-Functionality 执行此操作,但根本无法弄清楚如何与名称为“Name”的兄弟属性节点进行比较...我也愿意如果您知道 Count() 的替代方法,还有其他想法。

【问题讨论】:

  • 您需要说明这是 XSLT 1.0、2.0 还是 3.0。它会影响答案。

标签: xslt xpath count


【解决方案1】:

我不确定我是否完全理解了这个问题,所以请查看下方,如果这不是您真正想要的,请告诉我

首先可以使用下面的XPath

boolean(//SubRecord[substring-after(./Property[@Name="Derivation"]/text(), '.')=./Property[@Name="Name"]/text()])

这应该返回true,因为XPath 匹配前两个SubRecord 元素。

使用not() 应该允许返回true,因为XPath 匹配第三个SubRecord 元素:

boolean(//SubRecord[not(substring-after(./Property[@Name="Derivation"]/text(), '.')=./Property[@Name="Name"]/text())])

【讨论】:

  • 非常感谢,这正是我所需要的!
猜你喜欢
  • 2021-02-03
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多