XSL axis definitions

XSL allows us to traverse a document instance's graph in different directions. We start with a memo document instance:

<memo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="memo.xsd" date="9.9.2099">
  <subject>Ignore me!</subject>
    <para>Dumb text.</para>

This instance defines four nodes of type <to>. For each of these we want to create a line of text showing also the preceding and the following recipients:

 <----Jack----> Eve Jude Tolstoi ❶
Jack  <----Eve----> Jude Tolstoi ❷
Jack Eve  <----Jude----> Tolstoi ❸
Jack Eve Jude  <----Tolstoi----> 

Jack has no predecessor and 3 successors

Eve has 1 predecessor and 2 successors

Jude has 2 predecessors and 1 successor

Tolstoi has 3 predecessors and no successor

XSL supports this type of transformation by supplying XPath axis definitions. We consider a memo document with 9 <to> nodes:

Figure 896. A memo with 9 recipients Create comment in forum

We marked the 4-th recipient to represent the context node. All three <to> nodes to the left belong to the set of preceding siblings with respect to the context node. Likewise the 5 neighbours to the right are called following siblings. Returning to our four recipient example we may create the desired output by:

<xsl:template match="/">
  <xsl:apply-templates select="memo/to"/>

<xsl:template match="to">

  <xsl:for-each select="preceding-sibling::to" ❶>
    <xsl:value-of select="."/>
    <xsl:text> </xsl:text>

  <xsl:text> &lt;----</xsl:text>
  <xsl:value-of select="."/> <xsl:text>----&gt; </xsl:text>

  <xsl:for-each select="following-sibling::to"> <xsl:value-of select="."/>
    <xsl:text> </xsl:text>
  <xsl:value-of select="$newline"/>

Iterate on the set of recipients left of the context node.

Taking the context node's value embedded in <---- ... ---->.

Iterate on the set of recipients right of the context node.

See Figure 669, “Disjoint XPath axis definitions. ” for XPath explanations.

Some remarks:

  • If the context node is already the topmost node i.e. the root node then the sets defined by ancestor and parent are empty.

  • The parent set always contains zero or one node.