XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / XPath – Filtering sequences / Filtering a sequence

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

Testing for an attribute or child element

A simple predicate tests whether a child element or attribute exists, regardless of value. For example,

select="beispiel[test]"

selects every element node <beispiel> having a child element <test>, regardless of content. The same applies to attributes:

<xsl:apply-templates select="buch[@kat]"/>

selects all books in our example with a kat attribute. This filter is ineffective here because the attribute has a default value; even if absent in the source, the XML parser supplies it before the XSLT processor checks the predicate. It cannot distinguish a source attribute from an inserted default.

Testing equality or inequality against a value

To list books whose prices are in dollars, filter while constructing the sequence. Let the current node be root element <buchhandel>:

<xsl:apply-templates select="buch[preis/@waehrung='Dollar']"/>

The predicate contains an XPath expression evaluated for each node in the step’s result sequence to a Boolean: a book either has a child <preis> with attribute waehrung whose value is "Dollar", and remains in the sequence, or the attribute differs or is absent and the node is discarded. Every book is tested.

Caution:
Since the whole XPath expression is enclosed in double quotes, use single quotes around the attribute value inside the predicate:

buch[buchverlag='Ozean Verlag']

Simple position tests

Sequence items are numbered in order and accessible by index. The first index is always 1 for the first item. Use an index directly as a filter by supplying the relevant integer as the predicate expression. Thus,

buch[1]

passes only the first item from all book elements selected by the node test, here the first book in document order. This can also appear inside another predicate. To restrict a name comparison to the first <buchautor> element, for a reason explained later, write

buch[buchautor[1]='Lyon, Matthew']

, which retains books naming Matthew Lyon in their first <buchautor> element. In this example he is a coauthor listed second, so the result is empty.

This predicate form accepts exactly one integer xs:integer. Trying two simultaneous index numbers, such as beispiel[1 3], to select multiple items fails with a syntax error. Nor does an integer sequence such as 2 to 5 work. This expression does produce sequence (2, 3, 4, 5), but unfortunately beispiel[2 to 5] does not select items two through five. Instead, it passes every item without filtering. A solution appears on the following pages.

A supplied sequence, as soon as it is nonempty, always yields Boolean true. This is not a syntax error: a sequence is allowed, merely unhelpful.

Comparing less-than and greater-than values

Predicates can also compare greater-than or less-than values. Operator symbols > and < cannot be used directly in a stylesheet because the XML parser would mistake them for element delimiters. Instead use entities &gt; and &lt;.

The XML parser replaces entities before the XSLT processor starts, so the processor sees the correct symbols.

To output only books published before 2000, write:

select="buch[@ersch-jahr &lt; '2000']"

   

back to top