XSLT

Overview

* XSL stands for EXtensible Stylesheet Language.
* XSLT stands for XSL Transformations
* XSLT transforms an XML document into another XML document
* XSLT uses XPath to navigate in XML documents
* XSLT is a W3C Recommendation

Top Level Elements

xlt:import and xlt:include

* Both reference another style sheet.
* Style sheet imported with xlt:import has lower priority so current style sheet can override imported style sheet.
* xlt:import can only appear at the beginning of style sheet.

xlt:strip-space and xlt:preserve-space

<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="greeting salutation"/>

Preserve XML indents

Often times you want to preserve the xml output for readability. Here is how to achieve it.

<xsl:output method="xml" indent="yes"/>

xlt:key

xsl:variable

xsl:param

Extend XSLT

You can extend XSLT by
* Extension element
* Extension function

XPath

XPath 2.0

* Everything is sequence.
* Supports atomic values such xs:integer which can not be broken down to smaller parts.
* Supports all build-in data types of XML schema.

XPath Node Types

* The document node
– One per document
– No parent
* Element nodes
* Attribute nodes
* Text nodes
* Comment nodes
* Processing instruction nodes
* Namespace nodes

Node Tests

* node()
– Matches any node.
* text()
– Matches text nodes only.
* comment()
– Matches comment nodes only.
* processing-instruction()
– Matches processing instructions only.
* NCName:*
– Matches all nodes in a particular name space.

XPath 2.0 Node Tests

* element()
– any element
– same as select=”*”
– element(author): matches any element named author.
– element(date-of-birth, xs:gYear): matches any element named date-of-birth with data type of xs:gYear.
– element(*, xs:gYear): matches all elements of data type xs:gYear.
* schema-element(author)
– Matches any element named author which is declared in schema.
* attribute()
– same as select=”@*”
* *:NCName
– Matches all nodes with local name of NCName in any namespace.
* document-node()
– Matches a document node

Output Text

* Use xsl:text for more control over white spaces.
* Use <xsl:value-of select=””/> to output values of something.

<xsl:value-of select="count(/greetings/greeting)"/>

Branching

* xsl:if
* xsl:choose
* xsl:for-each

Invoke Template by Name

  <xsl:template match="/">
     <xsl:call-template name="t2"/>
  </xsl:template>
 
  <xsl:template name="t1" match="greeting">
        this is template 1.
  </xsl:template>
 
  <xsl:template name="t2" match="greeting">
        this is template 2.
  </xsl:template>

Template a la Mode

  <xsl:template match="/">
    <html>
      <body>
        A la templates.
        <xsl:apply-templates select="/greetings/greeting" mode="alat1"/>
        <xsl:apply-templates select="/greetings/greeting" mode="alat2"/>
      </body>
    </html>
  </xsl:template>
 
  <xsl:template mode="alat1" match="greeting">
      this is a la template 1.
      <xsl:value-of select="."/>
  </xsl:template>
 
  <xsl:template mode="alat2" match="greeting">
      this is a la template 2.
      <xsl:value-of select="."/>
  </xsl:template>

Sample Program To Do XSLT

package testxslt;
 
import java.io.File;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
 
public class TestXSLT1 {
    public TestXSLT1() {
    }
 
    public static void main(String[] args) throws TransformerConfigurationException, 
                                                  TransformerException {
        TestXSLT1 testxslt = new TestXSLT1();
        testxslt.doTransform("HelloWorld.xml","HelloWorld.xsl");
        testxslt.doTransform("HelloWorld.xml","HelloWorld2.xsl");
    }
 
    private void doTransform(String xml, String xsl) throws TransformerException, 
                                      TransformerConfigurationException {
        Source xmlSrc = new StreamSource(new File(xml));
        Source xsltSrc = new StreamSource(new File(xsl));
        Result result = new StreamResult(System.out);
 
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t;
 
        t = tf.newTransformer(xsltSrc);
        t.transform(xmlSrc,result);
    }
}

Examples

* Output Indented XML format

 <xsl:output method="xml" indent="yes"/>

* Suppress empty and null nodes

<!-- Macro template -->
<xsl:template name="writeElement">
  <xsl:param name="innode"/>
  <xsl:param name="outname"/>
  <xsl:if test="$innode[normalize-space(.) != ''] and $innode/text() != 'NULL'">
    <xsl:element name="{$outname}">
      <xsl:value-of select="$innode"/>
    </xsl:element>
  </xsl:if>
</xsl:template>
 
<!-- Use maco template -->
<xsl:call-template name="writeElement">
  <xsl:with-param name="innode"
                  select="myns:myOriginalNode"/>
  <xsl:with-param name="outname" select="'myTransformedNode'"/>
</xsl:call-template>

References

* XSLT Tutorial
* Java and XSLT

This entry was posted in xslt. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.