XSLT counting elements with a given value

后端 未结 3 767
自闭症患者
自闭症患者 2021-01-01 14:34

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:

EDIT: I upd

相关标签:
3条回答
  • 2021-01-01 14:42

    This XPath:

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

    returns the same value as:

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

    ...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'])
    

    because it means the same thing as:

    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 (after question edit)


    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!

    Here you go:

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

    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.

    0 讨论(0)
  • 2021-01-01 14:43
    <xsl:variable name="count" select="count(/Property/long = $parPropId)"/>
    

    Un-tested but I think that should work. I'm assuming the Property nodes are direct children of the root node and therefor taking out your descendant selector for peformance

    0 讨论(0)
  • 2021-01-01 14:52

    Your xpath is just a little off:

    count(//Property/long[text()=$parPropId])
    

    Edit: Cerebrus quite rightly points out that the code in your OP (using the implicit value of a node) is absolutely fine for your purposes. In fact, since it's quite likely you want to work with the "Property" node rather than the "long" node, it's probably superior to ask for //Property[long=$parPropId] than the text() xpath, though you could make a case for the latter on readability grounds.

    What can I say, I'm a bit tired today :)

    0 讨论(0)
提交回复
热议问题