Tuesday, 7 December 2010

XSLT style sheet

How can XML be styled using XSLT style sheets?  What are the strengths and weaknesses of using XSLT style sheets?  Give example to illustrate your answers.

# XSLT stands for Extensible Stylesheet Language Transformation. It is declarative, XML-based language used for the transformation of XML documents into other XML documents. It is also used to convert XML data into HTML or XHTML documents for display as a web page. XML is the partition between content and the layout achievable through applying external CSS or XSL style sheets to XML documents. However, since work started on XSL, the centre of attention has shifted from presentation towards transformation. It has given a birth to XSLT. It is much more widely used than its formatting counterpart, XSL formatting objects. http://www.xml.com/pub/a/2000/07/26/xslt/xsltstyle.html
It has some templates rules which have a pattern of identifying the nodes it matches and a template to be instantiate and output when the pattern is matched. During the process of transforming an XML document using an XSLT style sheet, XSLT walks the XML document tree, looking at each node in turn.
The processor compares each node on the XML document with the guide of each template rule in the style sheet and if the processor finds a node that matches a template rule's pattern, it outputs the rule's template. For instance:
<?xml version="1.0" encoding="UTF-8"?>
 <MiddlesexUniversity>
  <Student username="KM993">
    <name>Kshitij</name>
    <ID>M00310873</ID>
  </Student>
  <Student username="BG984">
    <name>Bidya</name>
    <ID>M00310863</ID>
  </Student>
</MiddlesexUniversity>

A transformation of his document operates on its corresponding node tree. The tree of nodes always begins with a root node for XML document which represents the document itself. In the given example, the child nodes of root can be the single document element node <MiddlesexUniversity >. The child nodes of document element can be any blending of text nodes and element nodes. Due to which, each of which may have same child nodes. This nesting of nodes forms a tree.

Transforming the given XML document using XSLT which provide template to do so:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="/ MiddlesexUniversity ">
    <root>
      <xsl:apply-templates select="Student"/>
    </root>
  </xsl:template>

  <xsl:template match="Student">
    <name username="{@username}">
      <xsl:value-of select="name" />
    </name>
  </xsl:template>
 </xsl:stylesheet>
Its assessment results to a new XML document:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name username="KM993">Kshitij</name>
  <name username="BG984">Bidya</name>
</root>

To start the process, the XSLT processor begins with a node list containing only the document root. It finds the template matching this root node—typically the rule with match="/"—and instantiates the contents of the template in the result tree. If the template contains elements from the XSLT namespace that select other nodes to process, the sequence of matching and template content instantiation continues recursively until there are no nodes left to process. When processing is completed, the result tree represents the target document produced by the transformation.
Some of its strength and weakness are as follows:
Strength:
  • XSLT is native to XML
  • Easy to merge XML data into a presentation
  • More resilient to changes in the details of XML document
  • It is a very powerful language as a little code can do a lot
  • Reasonably fast

Weakness:
  • It is suited to handling what’s marked up not what isn’t
  • Memory intensive and suffers a performance penalty
  • Difficult to implement complicated business rules
  • Have to learn a new language if not already familiar
  • Can’t change the value of variables (require recursions)


No comments:

Post a Comment