XSLT(Extensible Stylesheet Language Transformation)


XSLT (Extensible Stylesheet Language Transformation) is a language for transforming XML document into XML, HTML, XHTML, or plain text documents. XSLT serves us many ways:
1. It is a general-purpose translation tool.  It convert from one XML markup vocabulary to another. XSLT transform an XML document into HTML or XHTML so it will easily display in a web browser.
2. It can recognize document content
3. It can generate multiple results from the same content-like HTML, SVG and WAP
4. It uses XPath for navigating XML documents. XPath helps XSLT identify and find nodes in XML documents. Nodes are things like elements, attributes, and other objects in XML.
5. It help us to perform complex sorting and linking easily.
XSLT will create a new XML document based on the content of existing xml file without changing it. It is most often used to convert xml data into web pages/PDF, between different xml schemas.  It is developed by World Wide Web Consortium(W3C). The latest version is 2.0.

In XML document the order of element is meaningful but the order of attribute is not meaningful, comments are same as html comment, CDATA sections often used to ‘markup’ into an element. Like –

<Name><![CDATA[John Smith]]></Name>

<City><![CDATA[Dhaka]]></City>
<State><![CDATA[Bangladesh]]></State>
<Country><![CDATA[Bangladesh]]></Country>
<Zip><![CDATA[1234]]></Zip>

Default Rules of XSLT Processing:

  • Document node: xsl:apply-templates
  • Element nodes: xsl:apply-templates
  • Text nodes: output text

From Architectural Point of View:

  • XML Parser – An XML Parser is the component that interprets the XML code. Codes cannot be understood without a parser. An XML parser provides vital information to the program on how to read the file. Parsers come in multiple formats and styles. They can be free-standing software, libraries, modules and classes. All browsers have built-in XML parsers.
  • XSLT Processor – The software responsible for transforming source trees into result trees using an XSLT stylesheet is referred to as the processor.
  • Serialize – serialize the xml document for view the final output

How Does The Parser Work:

Parsers can be a bit complex. The goal is to transform XML into a readable code. Computer hardware only knows one language. It is the software that turns all the different languages into a workable format. Software is smart, but computers by themselves are ignorant. When presented with a bunch of XML code, computer hardware has no clue what it means. Parsers convert that code into something the hardware will recognize.

How XSLT Processor Work:

  1. Loads the input document as a DOM tree (internally the processor optimizes DOM)
  2. Performs a depth-first walk of the input tree
  3. As it walks through the document, selects a template in the stylesheet for the current node
  4. Applies the template, which describes how to create zero, one, or more nodes in the output tree
  5. When the walk is completed, creates a new tree (the output tree) from the input tree and the rules in the templates
  6. Writes the output tree according to the HTML or XML syntax

How Does Serialize The Result:

After your application has evaluated an XPath or XQuery expression or performed a transformation with an XSLT stylesheet, you might want to write the output as an actual XML document represented as a file or as a Java™ string. The process of rendering results as an XML document is known as serialization.

Procedure

  • Serialize an XSequenceCursor.

    Your application can call the XSequenceCursor.exportSequence method to serialize a sequence that is represented by an instance of the XSequenceCursor interface. The arguments on this method are an instance of the javax.xml.transform.Result interface and optionally an instance of the XOutputParameters interface.

    If the instance of the Result interface is also an instance of the javax.xml.transform.stream.StreamResult class, the sequence is serialized as described in the XSLT 2.0 and XQuery 1.0 Serialization Recommendation. The StreamResult object can contain an instance of the java.io.Writer class or the java.io.OutputStream class, where the processor will write the serialized sequence.

    You can create an instance of the XOutputParameters interface by calling XFactory.newOutputParameters() and call the methods on that object to override the default serialization parameter settings.

    XFactory factory = XFactory.newInstance();
    XPathExecutable expr = factory.prepareXPath("/purchaseOrder/item[@price > 1000]");
    XSequenceCursor exprResult = expr.execute(new StreamSource(inputFile));
    
    System.out.println("Items purchased costing more than $1000");
    if (exprResult != null) {
        // Set indenting in order to pretty-print result
        XOutputParameters params = factory.newOutputParameters();
        params.setIndent(true);
        exprResult.exportSequence(new StreamResult(System.out), params);
    } else {
        System.out.println("None found");
    }

    You can also call one of the getOutputParameters() methods on an instance of the XSLTExecutable interface to get the serialization parameters that are associated with a particular output definitionin an XSLT stylesheet. Use the XSLTExecutable.getOutputParameters(javax.xml.namespace.QName) method to get the serialization parameters for a named output definition or the no-argument XSLTExecutable.getOutputParameters() method to get those of the unnamed output definition. You might want do this to perform some post-processing on the result of the transformation using the instance of the XSequenceCursor interface that the transformation produces before serializing the result. If you change the settings of the serialization parameters in the instance of the XOutputParameters interface returned by one of the XSLTExecutable.getOutputParameters() methods, it will not affect the output definition in the stylesheet.

    XFactory factory = XFactory.newInstance();
    XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl"));
    XSequenceCursor xformResult = style.execute(new StreamSource("purchase.xml");
    
    XOutputParameters params = style.getOutputParameters(new QName("my-output-definition"));
    params.setMethod(XOutputParameters.METHOD_XHTML);
    xformResult.exportSequence(new StreamResult("output.html"), params);

    Note that according to the XSLT 2.0 and XQuery 1.0 Serialization Recommendation, a serialization error results if the sequence that is to be serialized contains attribute nodes or namespace nodes. If the sequence that you need to serialize might contain attribute or namespace nodes, get the values of those nodes as strings or some other appropriate type and serialize those values instead.

  • Serialize a single item.

    You can also serialize just the current item in an instance of the XSequenceCursor interface by using one of the exportItem methods. The exportItem methods are inherited from the XItemView interface, so they can be called on an instance of that interface as well.

    As with the exportSequence method described above, the arguments of the exportItem method are an instance of the javax.xml.transform.Result interface and optionally an instance of the XOutputParameters interface. The effect of calling exportItem is identical to the effect of calling exportSequence with a sequence that consists of just the current item.

  • Serialize the result of a transformation or query directly.

    Your application can also serialize the result of an XSLT transformation or XQuery expression directly by supplying an instance of the javax.xml.transform.Result interface on the XSLTExecutable.execute method or XQueryExecutable.execute method. The serialization parameter settings are determined by the attributes of any applicable xsl:output declaration or xsl:result-document instruction in the case of an XSLT stylesheet, and are always the default values in the case of the result of an XQuery expression.

    XFactory factory = XFactory.newInstance();
    XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl"));
    style.execute(new StreamSource("purchase.xml"),
                  new StreamResult("output.xml"));

    If your application supplies an instance of the XResultResolver interface on a transformation, your application can direct each final result tree to a different destination.

  • Use identity transformation.

    You can use the XML API to transform XML data contained in an instance of javax.xml.transform.Source directly to an instance of a javax.xml.transform.Result. This is often referred to as an identity transformation. See Performing basic XSLT operations for an example.

You can also serialize your objects into XML and then apply to the transform XML Object Serializer to XSLT Transformer .

Resources:

1. XSL Transformations (XSLT) Version 2.0
2. Serializing the results
3. How an XSLT processor works
4. CSCI E-153, Web Development Using XML

Advertisements