Home : Tutorials : XML Tutorial


XML Style Sheet (XSL) Syntax

XSL is used to define, how the XML file should be converted so that it can seen in the required format in the Browser. For Example say in the XML file we declare a Tag called 

<showbold>This is The Heading Text </showbold>

and you want to show the Text inside these tags in a different Font and Color , then in your XSL file you would declare it as 

<xsl:template match="showbold">
          <b><font color="#FF0000" face="Arial" size="3">
                 <xsl:apply-templates/>
          </font></b>
</xsl:template>

Here as soon as the Tag "showbold" is encountered in the XML file, then automatically the Text is replaced with Different Font size and color in bold. This is just an example. In real time, some tags, will be creating Table and show the Data in a well formatted way. Also note that tag "showbold" is not part of the standard HTML and here you have defined a Custom Tag.


Declaring Sequences in XSL File

Sequences are  useful if you want to show the same kind of tags, declared multiple times in the XML file in a more suitable format like, using Tables. Let's consider the Following Example XML File. 

<?xml version="1.0" encoding="ISO8859-1" ?> 
<?xml-stylesheet type="text/xsl" href="Library_Catalog.xsl"?>

<LIBRARY> 
   <BOOK> 
        <TITLE>Professional JINI</TITLE> 
        <AUTHOR>Sing Li</AUTHOR>
        <PUBLISHER>Wrox Publications</PUBLISHER>
   </BOOK> 
   .... 
   ......

The above XML file has a Parent Tag called LIBRARY which has lots of Sub Tags called BOOK. Now while Displaying this XML file, say we want to Display it in the Form of a Table. Here's how we will be generating Sequence in the XSL file, 

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
  <html>
  <body>
    <table border="2" bgcolor="blue">
      <tr>
        <th>Title</th>
        <th>Author</th>
	<th>Publisher</th>
      </tr>
      <xsl:for-each select="LIBRARY/BOOK">
      <tr>
        <td><xsl:value-of select="TITLE"/></td>
        <td><xsl:value-of select="AUTHOR"/></td>
	<td><xsl:value-of select="PUBLISHER"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

As you can see in the above Example, we consider each BOOK Tag under Main Tag LIBRARY and in a Sequence we display the List of all Books. The Main difference is made by tag "xsl:for-each" which helps in iterating through all the BOOK tags. 

Also note that when we say xsl:value-of select="PUBLISHER , then it retrieves the Value given inside the Tags </PUBLISHER> within the current Tag <BOOK>.

Home  Previous Next


Home : Tutorials : XML Tutorial

 

Copyright© 1998-2004 All Rights Reserved. No portion of this site may be reproduced or redistributed without prior written permission from VistaEdge Technologies

All registered trademarks appearing on this site are the property of their respective owners. Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries. This site is not connected to Sun Microsystems, Inc. and is not sponsored by Sun Microsystems, Inc.