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

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

An XSLT transformation’s processing flow depends directly on the composition of the sequence being processed. As shown in the previous chapter, a path expression assembles the sequence, normally while processing instructions such as xsl:apply-templates, but also in instructions that output sequences directly, such as xsl:value-of.

A path expression consists of location steps. Each follows an axis and produces a sequence whose nodes become starting points for subsequent steps. If the first step produces three nodes, the path therefore branches into three for the next step.

The final sequence includes all nodes produced by the last step, or by its instances, whose number depends on the preceding step. Since this can be a large set containing unwanted nodes, it is important to apply filters early, individually at each intermediate step.

The example document: a book catalogue

The document used to demonstrate the following concepts consists of <buch> elements with attributes ersch-jahr and kat. Attribute kat is assigned in the DTD a default value of "edv":

<!ATTLIST buch 
ersch-jahr CDATA #IMPLIED
kat CDATA "edv">

Code example: kap02/2.01/buchhandel.dtd.

Most book elements do not explicitly specify it; it is inserted by the XML parser. Other information is in child elements. Element <preis> has an attribute waehrung indicating currency. Its default, also from the DTD, is "Euro", overridden for some books by "Dollar".

<buchhandel>
  <!-- mehr Bücher -->
  <buch ersch-jahr="1996">
    <buchtitel>Where Wizards Stay Up Late</buchtitel>
    <buchautor>Hafner, Katie</buchautor>
    <buchautor>Lyon, Matthew</buchautor>
    <buchverlag>Touchstone</buchverlag>
    <preis waehrung="Dollar">14.00</preis>
  </buch>
  <buch ersch-jahr="1950" kat="tiere">
    <buchtitel>Unter Korallen und Haien</buchtitel>
    <buchautor>Hass, Hans </buchautor>
    <buchverlag>Ozean Verlag</buchverlag>
    <preis waehrung="Euro">37.95</preis>
  </buch>
  <!-- mehr Bücher -->
</buchhandel>

Code example: kap02/2.01/buchhandel.xml.

Processing every <buch> element is straightforward using the method already shown. But if that is not desired, unwanted book records should be filtered out while constructing the sequence, within the path expression.

   

   

back to top