Tutorial: Working the hierarchy
Zoo tutorials: [ SQL | Java | Linux | XML ]

A Gentle Introduction to xml

Working the hierarchy

Question 1: Using only the root node.

One option is to match just the root node. The template is only activated once. This simple template can be useful in many situations. In many more cases this is the basis of a more complex arrangement.
Notice that we have added some structure to our stock node. The items are now organised into a hierachy which reflects the departmental structure.
Notice that the price is being pulled in from the XML file.
  • Change the colour of the price to make it blue instead of red.
  • Change the price of the burgers to 62
  • Change the advert so that it is now about item 2 from department 1 - "Vile Sausages - less than 5% fat free!" You can put item[2] in place of item to select the appropriate price.
  • Change the advert so that it shows the price of the item with BarCode E6 - "Soggy Crisps: Cheese and Onion".

XML


XSL


Question 2: Explicit recursion.

Having matched a node the translator can be made to process it's children with the <xsl:apply-templates/> instruction. This allows us to embed generated text in a more flexible way. The template shown treats each of the three levels (stock, department and item) differently.
  • Add the fields BarCode and price to the lowest level template.
  • Change the div nodes to table components so that a table is given for each department. You will be able to ditch the top level template.
    <?xml version="1.0" encoding="UTF-8"?>
    
     <table><caption>meat</caption>
      <tr><td>E1</td><td>Pr-Burger</td><td>50p</td></tr>
      <tr><td>E2</td><td>Vile Saus</td><td>55p</td></tr>
     </table>
     <table><caption>junk_food</caption>
      <tr><td>E5</td><td>Crisp S+V</td><td>15p</td></tr>
      <tr><td>E6</td><td>Crisp C+O</td><td>15p</td></tr>
      <tr><td>E7</td><td>Flat Cola</td><td>50p</td></tr>
     </table>
    

XML


XSL