Thursday, May 29, 2014

XSLT Example [XML][(X)HTML][XSLT][Note]

XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.
XSLT stands for XSL Transformations. We'll be using XSLT to transform a sample XML document into XHTML.

If you want to take a quick tutorial about XSLT visit here.

Our sample XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sheet.xsl"?>

<People>
  <Person>
    <ID>1</ID>
    <Name niceName = "false">A Guy</Name>
    <Address>Streets</Address>
    <Company>None</Company>
    <EMail>some@person.com</EMail>
    <Info show  = "yes">Likes Candies</Info>
  </Person>
  
  <Person>
    <ID>2</ID>
    <Name niceName = "true">Another Guy</Name>
    <Address>Streets</Address>
    <Company>Some Company</Company>
    <EMail>some@otherPerson.com</EMail>
    <Info show = "no">Likes Ladies</Info>
  </Person>
</People>
And we'll be using this stylesheet with the name 
"sheet.xls"

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/People">
   <html>
   <body>
   <center>
        <h2 style="color:red;">My Contacts</h2><br/>
   </center>
        <h3 style="color:green;">Simple Element Value Display for All Persons</h3>
        <xsl:for-each select="Person">
          <xsl:sort select="ID" order="ascending"/>
          <em style="color:blue;">Name: </em><xsl:value-of select="Name"/><br/>
          <em style="color:blue;">Address: </em><xsl:value-of select="Address"/><br/>
          <em style="color:blue;">Company: </em><xsl:value-of select="Company"/><br/>
          <em style="color:blue;">EMail: </em><xsl:value-of select="EMail"/><br/>
        <br/>
        </xsl:for-each>
        
        <h3 style="color:green;">Display Person Details only if test="Name[@niceName = 'false']</h3>
        <xsl:for-each select="Person">
         <xsl:if test="Name[@niceName = 'false']">
          <em style="color:blue;">Name: </em><xsl:value-of select="Name"/><br/>
          <em style="color:blue;">Address: </em><xsl:value-of select="Address"/><br/>
          <em style="color:blue;">Company: </em><xsl:value-of select="Company"/><br/>
          <em style="color:blue;">EMail: </em><xsl:value-of select="EMail"/><br/>
        <br/>
         </xsl:if>
        </xsl:for-each>
        
        <h3 style="color:green;">[Choose] Display only if Info[@show='yes']</h3>
        <xsl:for-each select="Person">
        <xsl:choose>
         <xsl:when test="Info[@show = 'yes']">
          <xsl:value-of select ="Name"/>'s<em style="color:blue;"> Info: </em><xsl:value-of select="Info"/>
         </xsl:when>
         <xsl:otherwise>
          <xsl:value-of select ="Name"/>'s<em style="color:blue;"> Info: </em><em style="color:green;">You don't say</em>
         </xsl:otherwise>
         </xsl:choose>
        <br/>
        </xsl:for-each>
        
        <h3 style="color:green;">[sort] Display according to Decreasing ID numbers.</h3>
        <xsl:for-each select="Person">
        <xsl:sort select="ID" order="descending"/>
          <em style="color:blue;">Name: </em><xsl:value-of select="Name"/><br/>
          <em style="color:blue;">Address: </em><xsl:value-of select="Address"/><br/>
          <em style="color:blue;">Company: </em><xsl:value-of select="Company"/><br/>
          <em style="color:blue;">EMail: </em><xsl:value-of select="EMail"/><br/>
        <br/>
        </xsl:for-each>
        
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>


This will get us the output page:




No comments:

Post a Comment