XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / String values and attribute values / Using the attribute axis

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)

If attribute values belonged to an element’s string value, their reading order would be a problem: attributes are unordered by definition and lie outside document order.

Attributes are often regarded as following their source-code order, but this is unreliable, particularly for attributes inserted as defaults by the XML parser based on a DTD or schema.

A sequence of all attribute values can therefore be formed, but which two-item sequence from the preceding example is returned, (Meier, Peter) or (Peter, Meier), is unpredictable and left to the processor.

Since every node in a document tree must follow document order, this is a good reason to omit attribute nodes from the tree and place them on a separate, invisible attribute axis. (Note: The namespace axis also points outside the tree, but is no longer officially supported in XPath 2.0.) Attributes nevertheless belong to a specific element, their parent node.

An element can be found from its name and position. An attribute additionally requires leaving the ordinary tree structure, using a location step to switch to the attribute axis with axis name @. This step is also relative to the context.

If the context for

<person nachname="Meier" vorname="Peter"/>

is already on element <person>, a path expression can read its attributes directly. Moving from an element node to an attribute node is therefore a separate location step. Here this is done with

<xsl:value-of select="@nachname"/>

or:

<xsl:value-of select="@vorname"/>

The value read this way is also a string value.

The first expression creates a text node containing “Meier” in the result document, and the second one containing “Peter”.

   

back to top