Share on Google+Share on Google+

An XSLT Stylesheet

Advertisement
XSLT document is a well formed XML document which defines how the transformation of an xml document should take place.

XSLT document is a well formed XML document which defines how the transformation of an xml document should take place.

Let's take an example:

articles.xml

<?xml version="1.0"?>
<?xml-stylesheet href="article-xsl.xsl" type="text/xsl"?>
    <articles>
        <article>
             <title>What is XML?</title>
             <url>http://roseindia.net/tutorial/xml/what-is-xml.html</url>
        </article>
        <article>
             <title>Hello World XML</title>
             <url>http://roseindia.net/tutorial/xml/helloworldxml.html</url>
        </article>
</articles>

You can see the line below in the above xml.

<?xml-stylesheet href="article-xsl.xsl" type="text/xsl"?>

This line indicates that the this XML document should be transformed using article-xsl.xsl.

article-xsl.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html"/>
      <xsl:template match="article">
          <html>
              <body>
                  <b><xsl:value-of select="title" /></b>
                  <br/>
                  <xsl:value-of select="url" />
              </body>
          </html>
      </xsl:template>
</xsl:stylesheet>

You can see the line below in above document which defines that the resulting output will be HTML.

 <xsl:output method="html"/>

You can see the line below in above document. The template tag contains match attribute which indicates that this template will be applicable to the article node of the XML document.

 <xsl:template match="article">

You can see the line below in above document. The value-of tag has a select attribute which indicates to a specific element or group of elements within the XML document and produce output the value of the element. For example, in our case the below line outputs the value of the title element in XML file.

 <xsl:value-of select="title" />

The output of the above xml file in browser looks as:

 

Whitespace and text

Whitespaces are rendered as it is in template so take care of it while writing templates in XSLT document.

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:template match="article">Article Found</xsl:template>
</xsl:stylesheet>

The above document will result into the following output.

But if we write template as below using text tag. text tag ignores the whitespaces between template and text tags.

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:template match="article">
           <xsl:text>Article Found</xsl:text>
      </xsl:template>
</xsl:stylesheet>

Now the output change as below:

Advertisements

Advertisement

Posted on: November 26, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.