我需要计算XML文件中具有特定值的元素数量(以验证唯一性)。 XML文件如下所示:
编辑:我已更新原始“简化”的XML,使用了实际混乱的设计。不幸的是,这将使所有以前的答案变得混乱和错误,除非进行编辑。
<root>
<ac>
<Properties>
<Property Name="Alive">
<Properties>
<Property Name="ID">
<Properties>
<Property Name="Value">
<long>11007</long>
</Property>
</Properties>
</Property>
</Properties>
</Property>
<Property Name="Dead">
<Properties>
<Property Name="ID">
<Properties>
<Property Name="Value">
<long>11008</long>
</Property>
</Properties>
</Property>
</Properties>
</Property>
...
<Property Name="MostlyDeadAllDay">
<Properties>
<Property Name="ID">
<Properties>
<Property Name="Value">
<long>99001</long>
</Property>
</Properties>
</Property>
</Properties>
</Property>
</Properties>
</ac>
</root>
我试图定义一个变量,以查看在模板参数中定义的长值(ID)有多少个处于生/死级别的属性。以下是类似的代码(尽管我怀疑这是错误的)...
<xsl:param name="parPropId"/>
<xsl:variable name="countProperties">
<xsl:value-of select="count(/root/ac/
Properties/Property/
Properties/Property[@Name = 'ID']/
Properites/Property[@Name = 'Value']/long = $parPropId)"/>
</xsl:variable>
在“ID”级别下可以定义多个属性元素。但我相当确定,在“ID”下只能有一个属性元素(“Value”),在“Value”下只能有一个“long”元素。
[免责声明]设计整个XML文件的人真的不知道如何构建XML结构(例如,反向使用属性与元素)。恐怕我的XSLT思维因此暂时被扭曲了:)