Are '// myNode' and '// aNode // myNode' the same?
Yes, in this case, since all myNodes are also descendants of anode . However, in the general sense, //aNode//myNode obviously does not correspond to nodes that do not have anode parent in their ancestor tree.
xpath:
will ignore any intermediate hierarchy between anode and myNode , i.e. it will match /aNode/myNode , /anyNodes/anode/myNode and /anyNodes/anode/xyzNode/myNode
Which answers your last question, you can find the nodes in an interesting approach like this: (and again, ignoring any intermediate elements in the hierarchy)
//anode//interestingTree//myNode
Ideally, of course, you should be as clear as possible in your path, as // can incur overhead on performance due to the potentially large number of elements that need to be performed to search.
Edit Maybe this helps?
I changed your xml input for clarity to:
<root> <anode> <interestingTree> <unknownTree> <myNode> MyNode In Interesting Tree </myNode> </unknownTree> </interestingTree> <nonInterestingTree> <unknownTree> <myNode> MyNode In Non-Interesting Tree </myNode> </unknownTree> </nonInterestingTree> </anode> <anode> <someOtherNode/> </anode> <bnode> <myNode> MyNode in BNode </myNode> </bnode> </root>
When analyzing through a stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> Matched by `//myNode` <xsl:apply-templates select="//myNode"> </xsl:apply-templates> Matched by `//aNode//myNode` <xsl:apply-templates select="//anode//myNode"> </xsl:apply-templates> Matched by `//aNode//interestingTree//myNode` <xsl:apply-templates select="//anode//interestingTree//myNode"> </xsl:apply-templates> </xsl:template> <xsl:template match="myNode"> <xsl:value-of select="text()"/> </xsl:template> </xsl:stylesheet>
Returns the following:
Matched by `` MyNode In Interesting Tree MyNode In Non-Interesting Tree MyNode in BNode Matched by `` MyNode In Interesting Tree MyNode In Non-Interesting Tree Matched by `` MyNode In Interesting Tree