to the main page about the tutorial  THE XML REVOLUTION  -  TECHNOLOGIES FOR THE FUTURE WEB back up next

Different views

The following XSLT stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="collection">
    <nutrition>
      <xsl:apply-templates select="recipe"/>
    </nutrition>
  </xsl:template>

  <xsl:template match="recipe">
    <dish name="{title/text()}"
          calories="{nutrition/@calories}"
          fat="{nutrition/@fat}"
          carbohydrates="{nutrition/@carbohydrates}"
          protein="{nutrition/@protein}"
          alcohol="{number(concat(0,nutrition/@alcohol))}"/>
  </xsl:template>
</xsl:stylesheet>

produces a different view of the recipes:

<nutrition>
  <dish alcohol="0" 
        protein="32" 
        carbohydrates="45" 
        fat="23" calories="1167" 
        name="Beef Parmesan with Garlic Angel Hair Pasta"/>
  <dish alcohol="0" 
        protein="18" 
        carbohydrates="64" 
        fat="18" 
        calories="349" 
        name="Ricotta Pie"/>
  <dish alcohol="0" 
        protein="29" 
        carbohydrates="59" 
        fat="12" 
        calories="532" 
        name="Linguine Pescadoro"/>
  <dish alcohol="2" 
        protein="4" 
        carbohydrates="45" 
        fat="49" 
        calories="612" 
        name="Zuppa Inglese"/>
  <dish alcohol="0" 
        protein="39" 
        carbohydrates="28" 
        fat="33" 
        calories="8892" 
        name="Cailles en Sarcophages"/>
</nutrition>

which validates according to the DSD2 schema:

<dsd:dsd xmlns:dsd="http://www.brics.dk/DSD/2.0" 
         root="nutrition">

  <dsd:if><dsd:element name="nutrition"/>
    <dsd:declare><dsd:contents>
      <dsd:repeat><dsd:element name="dish"/></dsd:repeat>
    </dsd:contents></dsd:declare>
  </dsd:if>

  <dsd:if><dsd:element name="dish"/>
    <dsd:declare>
      <dsd:required><dsd:attribute name="name"/></dsd:required>
      <dsd:attribute name="calories"/>
      <dsd:attribute name="carbohydrates"/>
      <dsd:attribute name="protein"/>
      <dsd:attribute name="alcohol"/>
      <dsd:attribute name="fat"/>
    </dsd:declare>
  </dsd:if>

</dsd:dsd>

and using the XSLT stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns="http://www.w3.org/1999/xhtml">
 
  <xsl:template match="nutrition">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <link href="../style.css" rel="stylesheet" type="text/css"/>
      </head>
      <body>
        <table border="1">
          <tr>
            <th>Dish</th>
            <th>Calories</th>
            <th>Fat</th>
          <th>Carbohydrates</th>
          <th>Protein</th>
         </tr>
         <xsl:apply-templates select="dish"/>
      </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="dish">
    <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@calories"/></td>
      <td><xsl:value-of select="@fat"/>%</td>
      <td><xsl:value-of select="@carbohydrates"/>%</td>
      <td><xsl:value-of select="@protein"/>%</td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

then produces the following XHTML table:

DishCaloriesFatCarbohydratesProtein
Beef Parmesan with Garlic Angel Hair Pasta116723%45%32%
Ricotta Pie34918%64%18%
Linguine Pescadoro53212%59%29%
Zuppa Inglese61249%45%4%
Cailles en Sarcophages889233%28%39%

back COPYRIGHT © 2000-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH next