XSL1

Introduction to XSL

  • The transformations discussed in the preceding subsection are useful for converting a document between various XML representations.
  • Another type of transformation – extract information from one XML text document and use that information to create another XML text document.
  • The “programming language” used to direct this type of transformation is the Extensible Stylesheet Language (XSL).
  • An XSL document normally contains two types of information:
    • template data, which is text that is copied to the output XML text document with little or no change; and
    • XSL markup, which controls the transformation process.

 


 

  • Consider an XSL document is shown in Figure 7.12 and an xml document as shown in Figure 7.13.

// HW.xsl

<?xml version=”1.0″ encoding=”UTF-8″?>

<xsl:transform version=”1.0″

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”

xmlns=”http://www.w3.org/1999/xhtml”>

<xsl:template match=”/”>

<html>

<head>

<title>HelloWorld.xsl (transformed)</title>

</head>

<body>

<p><xsl:value-of select=”child::message” /></p>

</body>

</html>

</xsl:template>

</xsl:transform>

FIGURE 7.12 An example XSL document HW.xsl.

 

// HW.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<message>Hello World!</message>

FIGURE 7.13 Simple XML document HW.xml to be transformed.

 


 

  • The xsl document uses two namespaces.
    • XSL namespace name – http://www.w3.org/1999/XSL/Transform
    • XHTML namespace name – http://www.w3.org/1999/xhtml
      • So, in this document, XHTML is the default namespace, and elements prefixed with xsl belong to the XSL namespace.

 

  • Elements belonging to the XSL namespace are part of the XSL “programming language” and direct the transformation, while elements in other namespaces form part of the template data of the XSL document.

 

  • A JAXP Transformer instance can be used to perform XSL transformations.
  • The HW.xsl document is passed as a Source argument to program performing an XSL transformation along with an HW.xml document.
  • The program will output the content of the xsl:template element (template data) but with the xsl:value-of element replaced by the content of the XML document’s message element.

 

  • The result argument of the transform() method will receive the transformed document.
  •  


     

  • The output of the program is as shown below

<?xml version=”1.0″ encoding=”UTF-8″?>

<html xmlns=”http://www.w3.org/1999/xhtml”><head><title>

HelloWorld.xsl (transformed)

</title></head><body><p>Hello World!</p></body></html>

FIGURE 7.14 XHTML document produced by transforming HW.xml XML document according to HW.xsl XSL document.

 


 

// JAXP classes

import javax.xml.transform.*;

import javax.xml.transform.dom.*;

import javax.xml.transform.stream.*;

import javax.xml.parsers.*;

 

// DOM classes

import org.w3c.dom.*;

 

// JDK classes

import java.io.*;

 

/** Apply the XSL transform contained in the file named by the first command-line argument to the XML document named by the second argument and write the resulting document to standard output. */

 

class XSLTransform

{

public static void main(String args[])

{

try

{

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(new StreamSource(

new File(args[0])));

transformer.transform( new StreamSource(new File(args[1])),

new StreamResult(System.out));

}

catch (Exception e)

{

e.printStackTrace();

}

return;

}

}