Join wMUsers | Blog at wMUsers | User Control Panel | Site Map | webMethods Jobs |For Employers

Prabhu Palanisamy -- webMethods Ezine Columnist

Using XSLT to format XML documents



By Prabhu Palanisamy

 

Introduction to XSL

XSL (Extensible Stylesheet Language) is an XML-based language which converts XML documents into other formats including HTML, XML, or other styled documents. With all the characteristics of a programming language, XSL drastically reduces the effort required to represent XML data in a browser, a PDA device, or other medium.

XSL is W3C sanctioned (http://www.w3c.org/Style/XSL/) and is actually comprised of three separate languages:

  1. XSLT (Extensible Stylesheet Language Transformation) -- The langauge of the transformation
  2. XPath -- The language for navigating the XML DOM tree.
  3. XSL Formatting Objects -- an XML vocabulary for specifying formatting semantics

By using XSL, XML documents can be easily mapped to new schemas, reducing the effort required to pass information between different systems with different schemas.


Using XSL to create output documents
How XSL documents can be used to generate output documents


XSLT Transformations

XSLT transformation is the process of converting a source tree -- the XSLT processor's representation of the source XML document -- into a result tree -- the XSLT processor's representation of the formatted target document.

Source trees do not need to be read from file, either. Source trees can be read from local memory as a DOM. The same is true for result trees. Result trees can be written to memory and then later used as source trees for another process.

There are several transformation scenarios:

Input DocumentOutput Document
XML documentFormatted output document
XML documentResult tree
Result treeResult tree
Result treeFormatted output document

 

In most cases, an XSLT processor strictly reads an input document into a source tree and then applies an XSLT stylesheet to generate a formatted output document. The above table just demonstrates that their are unending possibilities for "daisy-chaining" XSLT transformations.


A Sample XSL Document

XSL documents should not look foreign to users of XML or HTML. The self-describing nature of the document is the same, but the tags have their own meaning. Below is a sample XSL document for drawing horizontal lines and a brief description of the document's tags.

XSL Tag Description

<xsl:stylesheet> -- The parent tag which encapsulates all the XSL tags.
<xsl:variable> -- To define a variable.
<xsl:template> -- A function module.
<xsl:if> -- If condition
<xsl:attribute> -- To define an attribute for the tag.
<xsl:value-of> -- To extract the value for a particular Tag.
<xsl:call-template> -- To call a template(Function)
<xsl:when> -- Do while loop
<xsl:for> -- For loop
<xsl:param> -- Used to define a variable for a function.

Sample XSL Document

<xsl:template name="drawHlines">
  <xsl:param name="startY" select="240"/>
  <xsl:param name="startYY" select="295"/>
  <xsl:param name="YNumber" select="1"/>
  <xsl:if test="($YNumber < 13)">

       <xsl:value-of select="$YNumber * 10 "/>
       <v:shape coordsize = "3460,2590"
                filled = "f"
                strokecolor = "#00c"
                strokeweight = ".96pt"
                path  = "m3280,2174 l320,2174 e">
        <xsl:attribute name="style">
        <xsl:value-of select="concat('WIDTH: 3460px;
                                      POSITION: absolute;
                                      HEIGHT: ',$startY)"/>
       </xsl:attribute>
      <v:stroke joinstyle = "round" endcap =  "round">
      </v:stroke>
      <v:fill></v:fill>
      <v:path></v:path>

          </v:shape>

            <P class="Chart" id="p">
       <xsl:attribute name="style">
       <xsl:value-of select="concat('font-weight:normal;
                                     FONT-SIZE: 7pt;
                                     LEFT: 12.6pt;
                                     WIDTH: 16.7pt;
                                     POSITION:absolute;
                                     text-align:center;',
                                     'TOP:',$startYY)"/>
      </xsl:attribute>
      <xsl:value-of select="295 - $startYY"/>
       </P>

      <xsl:call-template name="drawHlines">
      <xsl:with-param name="startY" select="$startY + 200"/>
      <xsl:with-param name="startYY" select="$startYY - 25"/>
      <xsl:with-param name="YNumber" select="$YNumber + 1"/>
       </xsl:call-template>
    </xsl:if>
</xsl:template>


Using the webMethods XSLT Module for XSL

webMethods offers an XSLT module for webMethods Integration Server v4.6 and higher. The following steps should be followed to download the package.

  1. Login to the webMethods Advantage site and select "Product Downloads".
  2. Locate the XSLT Module and download it.
  3. Install the package and activate it.
  4. Copy \packages\XSLT\code\jars\static\xalan.jar and \packages\XSLT\code\jars\static\xercesImpl.jar to the directory \lib\jars\.
  5. Restart the Integration Server.

Launch Developer and confirm that the XSLT package is installed. It is named "XSLT".

In the XSLT package, there are two main services:

  1. pub.xslt.Transformations:transformSerialXML -- a service accepting an XML document and an XSL stylesheet as inputs and which outputs a formatted document as bytes.
  2. pub.xslt.Cache:removeTemplate -- a service which removes the existing XSLT template from the server's cache.



[1]  2  Next>>

Go Deeper on the Subject: The wMUsers Discussion Forums


Prabhu Palanisamy is a Software Engineer working at Satyam Computer Services. He has in-depth knowledge in the areas of Web-based applications and B2B integration. He has strong passion for XML, XSL and has written various articles on the same.

Prabhu can be reached via email at


Advertise at wMUsers






  Home | Join wMUsers | Discussion Forums | Knowledge Center | Jobs | Shareware | User Groups | Links |
Contact Us | Terms of Service | Privacy Policy

wMUsers is an independent organization and is not sponsored in any manner by Software AG.


© All Rights Reserved, 2001-2008.