Tuesday 3 December 2013

eXtensible stylesheet Language [XSL]



What is XSL?
XSL stands for eXtensible Stylesheet Language which is much like XML. XSL is a language for expressing stylesheets. It consist of XSLT, is an application for specifying rules by which one XML document is transformed into another XML document; Xpath, Expression language used by XSLT to locate elements and attributes in an XML document. It also tests Boolean conditions, perform numerical calculations; XSL-FO (Formatting Objects), which specifies formatting properties for rendering the document to some other format.

Advantages:

Easy display formatted XML data in browser
Easier to modify when XML data format changes than to modify DOM and SAX parsing code
Can be used with database queries that return XML

Disadvantages:

Memory exhaustive, performance hit.
Difficult to implement complex business rules

XSLT:
eXetensible Stylsheet Language for Transformation specifies rules for conversion i.e. template or template rules. There are two types of template rules for transformation: Matching and selection templates, and Branching elements.

Matching and selection templates

xsl:template

xsl:apply-templates

xsl:value-of



Branching elements

xsl:for-each

xsl:if

xsl:choose



Matching and selection templates

xsl:template :

xsl:template match=“XPath:

Defines a template rule for producing output. It is applied only to nodes that match the pattern.
It is invoked by using <xsl:apply-templates/>


<xsl:template match="/">

<html><body>

<xsl:apply-templates/>

</body></html>

</xsl:template>

<xsl:template match=“name">

Your name is <xsl:value-of select=“first”/>

</xsl:template>

xsl:value-of select:

xsl:value-of select=“expression :

It evaluates the expression as a string and outputs the result. It is applied only to the first match. The “.” selects the text value of the current node.
e.g.
<name prefix=“Mr.”>Joo</name>

xsl:for-each select:

xsl:for-each select=“expression”:

 It processes each node selected by the XPath expression. It is applied only to the first match. The “.” selects the text value of the current node
e.g.
<class>

<student>kk</student>

<student>kkd</student>

</class>


xsl:if test:

xsl:if test=“expression”:

 It evaluates the expression and if true applies the template.

<xsl:template match=“class">

<!-- Select the first node in the set. -->

<xsl:if select=“position() = first()">

<b><xsl:value-of select=“."/></b>

</xsl:if>

</xsl:template>

xsl:choose :
 It selects any number of alternatives. It is used instead of if-else, or switch statement used in other programming languages.

<xsl:choose>

<xsl:when test=“not(text())">

Missing value!

</xsl:when>

<xsl:otherwise>

<xsl:value-of select=“.”/>

</xsl:otherwise>

</xsl:choose>

ShareThis

Related Posts Plugin for WordPress, Blogger...