Friday 25 March 2016

Lets learn XSLT....

Xtrans is very lightweight tool for practising  XSLT.
Parallely try out examples in Xtrans tool.
There are other XSLT editors and Mapping tools but many good visual mapping tools are only proprietory.

You can also use 15 days trial versio of Stylus Studio XSLT mapper.I liked it a lot.
Even though we have many tools which does things automatic for us,its always good to know the syntax and meaning of each functions to be a good in XSLT.

An XSL style sheet consists of one or more set of rules that are called templates.

A template contains rules to apply when a specified node is matched.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:template match="/">

            </xsl:template>
</xsl:stylesheet>
Version is 1.0 always for http://www.w3.org/1999/XSL/Transform url.
The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
Try out some basix xslt functions listed below.Quick way to learn and Tryout is through W3 schools.
1)Template –when you do some template examples,also don’t miss to tryout examples with match and mode.
2)match – you need to little bit research on how to use Xpath in order to provide appropriate values in match.In “match”,you are giving xpath.
Some examples for your reference here:
<xsl:template match="//a[position() = 5]">
 <!-- this template matches the fifth a element
         anywhere in the document. -->
</xsl:template>

<xsl:template match="//div[@class='foo']/bar[position() = 1]">
 <!-- this template matches the first bar element that is
      a child of a div element with a class attribute equal to "foo" -->
</xsl:template>

So,according to the requirement,you can first find out proper Xpath and then use that in “match”.
3)For-each
4)Value-of
5)Sort
6)If
7)Choose
8)Apply-template
Some advanced xslt definitions:
1)select = "."
"." is the current node - it's value depends on which template or xslt instruction it is processing.
2) <xsl:output>
 The <xsl:output> element defines the format of the output document.
3) The <xsl:element> element is used to create an element node in the output document.
4) match=”*” vs  match=”node()”
Given xml:
<A>
    Text1
    <B/>
    Text2
</A>
Matching on node()
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
 
    <!--Suppress unmatched text-->
    <xsl:template match="text()" />
 
    <xsl:template match="/">
        <root>
            <xsl:apply-templates />
        </root>
    </xsl:template>
 
    <xsl:template match="node()">
        <node>
            <xsl:copy />
        </node>
        <xsl:apply-templates />
    </xsl:template>
</xsl:stylesheet>
Gives:
<root>
    <node>
        <A />
    </node>
    <node>
        Text1
    </node>
    <node>
        <B />
    </node>
    <node>
        Text2
    </node>
</root>
Whereas matching on *:
<xsl:template match="*">
    <star>
        <xsl:copy />
    </star>
    <xsl:apply-templates />
</xsl:template>
Doesn't match the text nodes.
<root>
  <star>
    <A />
  </star>
  <star>
    <B />
  </star>
</root>

5)Its interesting to know about recursive functions

<xsl:template match="*" mode="copy-no-namespaces">
  <xsl:apply-templates select=”node()” mode=”copy-no-namepaces”/>
</xsl:template>
 
<xsl:apply-templates select=”node()” matches <xsl:template match="*".So it becomes recursive.

6)Some basic patterns we need to know
xample patterns and what they match
Pattern
What it matches
/
the root node
/doc[@format='simple']
the root element only if its name is doc and it has a format attribute with the value simple
bar
any bar element
foo/bar
any bar element whose parent is a foo element
id('xyz')/foo
any foo element whose parent is an element that has an ID-typed attribute with the value xyz
section//para
any para element that has a section element ancestor
@foo
any attribute named foo
@*
any attribute
node()
any child node (i.e., element, text, comment, or processing instruction)
text()
any text node
*
any element

7)To get better understanding on apply-template and template,
Read this :

<xsl:apply-templates select="node()"/>
This instruction applies templates to the child nodes of the current node. In other words, it populates the current node list with the set of nodes returned by the node() expression, and it iterates over them in document order, invoking the best-matching template rule for each node.
Tip
In object-oriented (OO) terms, xsl:apply-templates is like a function that iterates over a list of objects (nodes) and, for each object, calls the same polymorphic function. 
  • xsl:apply-templates: lets you decide when and where your xsl:template elements are used

8)Using modes
Modes effectively allow you to define different template rules for the same node. In other words, you can process the same node two different times and do something different each time. A common use case for modes is generating a table of contents. Most of your template rules in a stylesheet might be concerned with generating the document content (headings, paragraphs, etc.) like this:
<xsl:template match="heading">
  <h1>
    <xsl:value-of select="."/>
  </h1>
</xsl:template>
However, to generate entries for a table of contents, you could define corresponding template rules in the toc mode:
<xsl:template match="heading" mode="toc">
  <li>
    <xsl:value-of select="."/>
  </li>
</xsl:template>
For heading elements, <xsl:apply-templates/> will generate h1 elements, and <xsl:apply-templates mode="toc"/> will generate li elements.