Let's say I have the following xml:
<root>
<person>
<name>John</name>
</person>
<children>
<person>
<name>Jack</name>
</person>
</children>
</root>
Is it possible to select both persons at once? Assuming that I don't know that the other person is in the children tag, they could easily be in the spouse tag or something completely different and possibly in an other child. I do know all persons I need are in the root tag (not necessarily the document root).
You can use
//person
or
//*[local-name()='person']
to find any person
elements in the document, but be careful - certain xsl processors (like Microsoft's), the performance of double slash can be poor on large xml
documents because all nodes in the document need to be evaluated.
Edit :
If you know there are only 2 paths to 'person' then you can avoid the //
altogether:
<xsl:for-each select="/root/person | /root/children/person">
<outputPerson>
<xsl:value-of select="name/text()" />
</outputPerson>
</xsl:for-each>
OR namespace agnostic:
<xsl:for-each select="/*[local-name()='root']/*[local-name()='person']
| /*[local-name()='root']/*[local-name()='children']/*[local-name()='person']">
In Petar Ivanov's answer the definition of //
is wrong.
Here is the correct definition from the XPath 1.0 W3C Specification:
//
is short for/descendant-or-self::node()/
//name
will match both, no matter where they are in the xml tree.
//
Selects nodes in the document from the current node that match the selection no matter where they are (link)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With