up previous next contents
Next: Enterprise Java Beans Up: Survey of other server-side Previous: XML   Contents

XSLT

  • eXtensible Stylesheet Language: Transformation
  • XSLT documents are instructions that describe how to transform one XML document into another type of XML document.
  • Example uses:
    • Converting the XML for an order into HTML that displays the order to the user.
    • Converting an XML invoice from a vendor's system into XML that your internal system understands.
  • This sample shows how to transform the sandwich XML from the previous section into HTML.
    Input XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <sandwich price="2.00">
      <meat>Pastrami</meat>
      <bread>Rye</bread>
      <cheese slices="1">Swiss</cheese>
    </sandwich>
    
    XSLT:
    <!-- XSLT is itself XML -->
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/sandwich">
      <!-- the HTML tags are embedded directly in the template -->
      <html>
      <head><title>My Sandwich</title></head>
        <body>
        <!-- when executed, the value-of element is replaced 
             with the actual value -->
        <b>Meat:</b> <xsl:value-of select="meat" /><br />
        <b>Bread:</b> <xsl:value-of select="bread" /><br />
        <b>Cheese:</b> <xsl:value-of select="cheese/@slices" /> 
            slice(s) of <xsl:value-of select="cheese" />
        <br /><br />
        <b>Cost:<xsl:value-of select="@price" /></b>
        </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>
    Output:
    <html>
    <head>
      <title>My Sandwich</title>
    </head>
    <body>
      <b>Meat:</b>Pastrami<br />
      <b>Bread:</b>Rye<br />
      <b>Cheese:</b>1 slice(s) of Swiss
      <br /><br />
      <b>Cost: 2.00</b>
     </body>
    </html>
    

  • The JAXP defines the API for executing XSLT transformations.


up previous next contents
Next: Enterprise Java Beans Up: Survey of other server-side Previous: XML   Contents