XSLT 计数具有给定值的元素 [英] XSLT counting elements with a given value

查看:22
本文介绍了XSLT 计数具有给定值的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算 XML 文件中具有特定值的元素数量(以验证唯一性).XML 文件如下所示:

I need to count the number of elements in an XML file that have a particular value (to verify uniqueness). The XML file looks like this:

我用某人设计的实际毛茸茸的混乱更新了原始的简化"XML.不幸的是,除非编辑,否则这将使之前的所有答案变得非常混乱和错误.

I updated the original "simplified" XML with the actual hairy mess someone designed. Unfortunately, this is going to make all the previous Answers really confusing and wrong unless edited.

<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).类似的东西(虽然我怀疑这是错误的)...

I am trying to define a variable to see how many of the properties at the Alive/Dead level have the long value (ID) as defined in a template parameter. Something along these lines (though I suspect this is wrong)...

<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"级别定义多个 Property 元素.但我相当确定我只能依赖ID"下的一个 Property 元素(Value"),而Value"下只能依赖一个long"元素.

There can be multiple Property elements defined at the "ID" level. But I am fairly certain I can count on just one Property element ("Value") under "ID", and only one "long" element under "Value".

[免责声明] 设计我一直在使用的整个 XML 文件的人真的不知道如何构建 XML(例如,向后使用属性与元素).我担心我的 XSLT 想法暂时被扭曲了 :) 结果.[/免责声明]

[disclaimer] Whoever designed the overall XML file I'm stuck working with REALLY didn't know how to structure XML (e.g., backwards use of attributes versus elements). I fear my XSLT thinking has temporarily been warped :) as a result. [/disclaimer]

推荐答案

此 XPath:

count(//Property[long = '11007'])

返回相同的值:

count(//Property/long[text() = '11007'])

...除了第一个计算符合条件的 Property 节点,第二个计算符合条件的 long 子节点.

...except that the first counts Property nodes that match the criterion and the second counts long child nodes that match the criterion.

根据您的评论并多次阅读您的问题,我相信您希望根据标准组合找到唯一性.因此,实际上,我认为您实际上是在检查多个条件.以下内容也适用:

As per your comment and reading your question a couple of times, I believe that you want to find uniqueness based on a combination of criteria. Therefore, in actuality, I think you are actually checking multiple conditions. The following would work as well:

count(//Property[@Name = 'Alive'][long = '11007'])

因为它的意思与:

count(//Property[@Name = 'Alive' and long = '11007'])

当然,您可以替换模板中参数的值.上面的代码只是说明了一点.

Of course, you would substitute the values for parameters in your template. The above code only illustrates the point.

EDIT(问题编辑后)

关于 XML 的可怕之处,您说得很对.事实上,这是一个彻头彻尾的 CodingHorror 候选人!我不得不不断地叙述以跟踪我目前所在的属性"节点.我感觉到你的痛苦!

You were quite right about the XML being horrible. In fact, this is a downright CodingHorror candidate! I had to keep recounting to keep track of the "Property" node I was on presently. I feel your pain!

给你:

count(/root/ac/Properties/Property[Properties/Property/Properties/Property/long = $parPropId])

请注意,我已删除所有其他检查(针对 ID 和值).它们似乎不是必需的,因为您可以使用 XML 中的层次结构到达相关节点.此外,您已经提到唯一性检查仅基于 long 元素的内容.

Note that I have removed all the other checks (for ID and Value). They appear not to be required since you are able to arrive at the relevant node using the hierarchy in the XML. Also, you already mentioned that the check for uniqueness is based only on the contents of the long element.

这篇关于XSLT 计数具有给定值的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆