Tutorial: XML and XSL
Zoo tutorials: [ SQL | Java | Linux | XML ]

A Gentle Introduction to xml


Question 1: Introducing templates

The XML contains a single stock element which in turn contains four items. We use this structure to investigate how XSL processes nodes.

The template is tested against every node in the input tree. When a match is made then the content of the template is output.

Once a match is made the child nodes will not be considered unless an explicit instruction to do so is given.

This is a simple transformation, it outputs a single constant value for every node matched.
  1. Try the sheet as it stands, notice that one match is made
  2. Change the match value from stock to item - notice that four matches are now made.
  3. Rather than output a constant value we commonly want to output values derived from the nodes. Try <xsl:value-of select="@price"/> in place of Found something!



Question 2: Investigating xsl:value-of

Typically we need to consider two issues in a template:
  • Which nodes do we want to match?
  • What do we want to output?

We can decide which nodes match using match attribute of the xsl:template node - the content of this node determines what gets output.

In this example we output the bar code and the description of each item.

The xsl:value-of nodes

  • Try it
  • Change the xsl so that the legend is given in italics.
  • Oops: make a mistake - what happens if you forget to put in a closing tag - such as the close italics tag. If the xsl is not well formed xml then we get an error.
  • Try each of the following values for the select attribute:
    • @price
    • name()
    • substring-before(@legend,' ') other string functions
    • position() - note that white space text nodes contribute to this count.
    • last()



Question 3: Deciding what to show

We can use the match attribute to decide which nodes are considered. There are a number of ways of performing matches.
  • Use match="item[@price=15]" to show only items costing 15p
  • Use match="item[position()=1]" to show only the first item
  • Use match="item[position()=last()]" to show only the last item
  • Use match="item[starts-with(@legend,'Crisp')]" to show...
  • Show only those items with an "r" in the legend (you will probably need to refer to the XPath specification



Question 4: Axis

This question introduces more advanced topics. You would be well advised to return to this question after having completed some of the later tutorials.

The default axis is the child axis - when we specify stock/item we mean that item is a child of stock. It is possible to employ another axis such as ancestor, following-sibling, preceding-sibling, following, preceding, namespace, descentant and ancestor-or-self.

  • The sample shows the legend attribute of the following sibling of the item with bar code E5. The output should be Crisp C+O
  • Use the phrase following-sibling::item/following-sibling::item to get the legend of the item after the item with bar code E5. That should be Flat Cola
  • Use the following axis to find the legend of the item with price 50 that comes after the item with bar code E5. The answer should be Flat Cola
  • Use the axis preceding to find the legend of the item with price 15 that comes just before the item with bar code E6. The answer should be Crisps S+V