[Previous] [Contents] [Next]


Matching an Element by its Ancestry


In XSL you can match an element according to what its parent element is (a context-sensitive match). For example:

<xsl:template match="line/stress">

matches any stress element that has a line element as its parent.

Instead of the immediate relationship of parent/child, you can select by the less precise ancestor selector. For example, the following would select a stress element that has a line element somewhere in the tree directly above it (great grandparent, grandparent, and so on). Note that there are two slashes, not one:

<xsl:template match="line//stress">

You can also make nested selections consisting of more than one parent. For example, the following would match a stress element that has a line element as its parent, and that line element has a poem element as its parent (the poem element must be the stress element's grandparent):

<xsl:template match="poem/line/stress">

[Previous] [Contents] [Next]