Constructing a Compound Location Path Using Union
Simple XPath location paths consist of one or more location steps, any or all of which can be quite complex.
The XPath specification allows an even broader reach through any document, by providing for compound location paths — multiple location paths, separated from one another by a vertical bar or pipe symbol (|). The resulting node-set represents the union of all nodes retrieved by all of its constituent location paths.
We see simple examples of compound location paths in the built-in template rules defined by the XSLT specification. For instance:
<xsl:template match="comment() | processing-instruction()
" />
matches the set of all nodes in the document which are of either the comment or the processing instruction node types. For more information about the built-in template rules, see How XSLT Template Rules Depend on Context.
Most often, compound location paths are used to simplify locating document content which comes from different branches of the document tree. For example, consider the example XML document shown below.
Although this document is structurally simple, using a single location path to obtain content from different branches of its tree of nodes can be extremely complicated.
You could, for example, locate the <primate>
element corresponding to "gibbon", the second <marsupial>
element (whatever it is), and all the <insect>
elements using a single location path consisting of multiple steps, utilizing various axes such as ancestor::
, parent::
, preceding-sibling::
, and child::
—perhaps together with the position()
or other functions. Such a location path would be difficult to construct in the first place, difficult to maintain, and quite possibly useless if intervening elements are later added to the document (or existing ones dropped).
It is much simpler to achieve this with a compound location path such as the match pattern in the example XSLT template rule below.
Example
XML File (animals.xml)
<?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="animals.xsl" ?> <animals> <insects> <insect>bee</insect> <insect>fly</insect> <insect>beetle</insect> <insect>ant</insect> </insects> <mammals> <marsupials> <marsupial>kangaroo</marsupial> <marsupial>opossum</marsupial> </marsupials> <primates> <primate>gibbon</primate> <primate>lemur</primate> <primate>gorilla</primate> <primate>chimpanzee</primate> </primates> </mammals> </animals>
XSLT File (animals.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- suppress text nodes not covered in subsequent template rule -->
<xsl:template match="text()"/>
<xsl:template match="*[.='gibbon'] | marsupial[2] | insect
">
<p><xsl:value-of select="."/></p>
</xsl:template>
</xsl:stylesheet>
Formatted Output
bee
fly
beetle
ant
opossum
gibbon
Processor Output
<?xml version="1.0" encoding="UTF-16"?><p>bee</p><p>fly</p><p>beetle</p><p>ant</p><p>opossum</p><p>gibbon</p>
For more information about the position()
function, see Processing Node-Sets by Using Node-Set Functions.