Example: memo recipients

We are now interested in a list of all recipients being defined in a memo element. We introduce the element xsl:for-each which iterates over a result set of nodes:

Figure 887. List of recipient nodes iteration. Create comment in forum
<xsl:template match="/" ❶>
  <xsl:for-each select="memo/to" ❷ >
    <xsl:value-of select="." ❸ />
    <xsl:text>,</xsl:text> </xsl:for-each>
</xsl:template>
Adam Hacker,Eve Intruder,

This template matches the XML document instance, not the visible <memo> node.

The XPath expression memo/to gets evaluated starting from the invisible top level document node being the context node. For the given document instance this will define a result set containing both <to> recipient nodes, see Figure 888, “Selecting node sets from memo document instances ”.

The dot . represents the #PCDATA content of the current to element.

A comma is appended. This is not quite correct since it should be absent for the last element.

Right now we do not bother about the trailing , following the last recipient. The surrounding <xsl:text>,</xsl:text> elements may be omitted. We encourage the reader to leave them in place since they increase readability when a template's body gets more complex. The element <xsl:text> is used to append static text to the output. This way we append a separator after each recipient. We now discuss the role of the two attributes match="/" and select=memo/to. Both are examples of so called XPath expressions. They allow to define node sets being subsets from the set of all nodes from a given document instance.

Conceptually XPath expressions may be compared to the SQL language the latter allowing the retrieval of datasets from a relational database. We illustrate the current example by a figure:

Figure 888. Selecting node sets from memo document instances Create comment in forum

This figure needs some explanation. We observe an additional node above <memo> being represented as filled. This node represents the document instance as a whole and has got memo as its only child. We will rediscover this additional root node when we discuss the DOM application programming interface.

As already mentioned the expression memo/to evaluates to a set of nodes. In our example this set consists of two nodes of type <to> each of them representing a recipient of the memo. We observe a subtle difference between the two XPath expressions:

match="/"

The expression starts and actually consists of the string /. Thus it can be called an absolute XPath expression. Like a file specification C:\dos\myprog.exe it starts on top level and needs no further context information to get evaluated.

A XSL style sheet must have an initial context node to start the transformation. This is achieved by providing exactly one <xsl:template> with an absolute XPath value for its match attribute like /memo.

select="memo/to"

This expression can be compared to a relative file path specification like e.g. ../images/hdm.gif. We need to add the base (context) directory in order for a relative file specification to become meaningful. If the base directory is /home/goik/xml than this relative file specification will address the file /home/goik/images/hdm.gif.

Likewise we have to define a context node if we want to evaluate a relative XPath expression. In our example this is the root node. The XSL specification introduces the term evaluation context for this purpose.

In order to explain relative XPath expressions we consider content/para starting from the (unique!) memo node:

Figure 889. XPath content/para starting from context node <memo>. Create comment in forum

(Animated PDF Version)

XPath content/para starting from context node <memo>.

The dashed lines represent the relative XPath expressions starting from the context node to each of the nodes in the result set.