Transformations API for XML
by , 11-07-2011 at 06:36 PM (1974 Views)
In the next few posts, I will write about how to perform an XSLT transformation using StAX APIs: Cursor API and Event Iterator API.
TrAX (Transformations API for XML) is a Java API for performing XSLT transformations. In J2SE 1.5, there are three different ways to represent the source and the result of an XSLT transformation and they are: by XML stream source/result, by SAX events source/result, and by DOM tree source/result. In J2SE 1.6 an XSLT transformation is performed using a StAX (Streaming API for XML) source as an input and a StAX result as an output.
To get started, you should have Java 1.6 installed and working on your machine. Also you should have some knowledge of XSLT and StAX.
Lets start with XSLT/TrAX transformation process.
Firstly, we have to obtain a javax.xml.transform.TransformerFactory object. This class provides an XSLT processor. To obtain its object, we will instantiate this class by calling the newInstance method.
We also have to set the javax.xml.transform.TransformerFactory system property:Java Code:TransformerFactory transfFactory=TransformerFactory.newInstance();
Java Code://setting the javax.xml.transform.TransformerFactory system property for the Saxon processor System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl"); //setting the javax.xml.transform.TransformerFactory system property for the Xalan processor System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
Second step is to provide a javax.xml.transform.Source object for the XSL stylesheet. For this, you have to instantiate the proper XXXSource class.
Now is the time to obtain a javax.xml.transform.Transformer object. This is done by calling the newTransformer method. This object will transform the source-tree into result-tree.
The newTransformer method expects the source of the XSL stylesheet as parameter.Java Code:public abstract Transformer newTransformer(Source source) throws TransformerConfigurationException
We also have to indicate the XML document for transformation. This is done by creating another Source object. For this one has to instantiate the proper XXXSource class.
Now is the time to create a javax.xml.transform.Result object. Also we have to decide what kind of result we will use and then we havet o instantiate the proper XXXResult class.
To start the transformationm Transformer.transform method is called. This method retrieves the Source of the XML document (that we wish to transform) and the Result of that transformation:
The six steps discussed here makes XSLT transformation based on the StAXSource/StAXResult classes very simple and easy. The actual task is to create the StAXSource/StAXResult objects and the using the required constructor.Java Code:public abstract void transform(Source XMLs, Result result)throws TransformerException
Time for an example. Suppose we have following XML file:
<!--?xml version="1.0" encoding="UTF-8" standalone="no" ?-->
<!--?xml-stylesheet type="text/xsl" href="xslFile.xsl"?-->
<member>
<name>Martin Hook</name>
<e-mail>mh@yahoo.com</e-mail>
</member>
Also we have the following XSL style sheet:
<!--?xml version="1.0" encoding="UTF-8"?-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.01" encoding="ISO-8859-1">indent="yes" media-type="text/html" />
<xsl:template match="/member">
Member
<font face="arial" size="3"><strong><em>Name:</em></strong></font>
<font face="arial" size="5"><xsl:value-of select="//name"></xsl:value-of></font>
<font face="arial" size="3"><strong><em>E-mail:</em></strong></font>
<font face="arial" size="5"><xsl:value-of select="//e-mail"></xsl:value-of></font>
</xsl:template>
</xsl:output></xsl:stylesheet>
To create StAXSource for the XSL, we use Cursor API as shown in the snippet below:
Java Code:XMLStreamReader streamReaderXSL=null; … //define the Source object for the stylesheet Source XSL=new StAXSource(streamReaderXSL);
Continuing with the example, we have following code for the XML document:
Transformation results in the following:Java Code:XMLStreamReader streamReaderXML=null; … //define the Source object for the XML document Source XML=new StAXSource(streamReaderXML);
If you want to use Event Iterator API, the code snippet presented below will be useful for you.Java Code:XMLStreamWriter streamWriter=null; … //define the Result object Result XML_r=new StAXResult(streamWriter);
Hope this helps.Java Code:XMLEventReader eventReaderXSL=null; //define the Source object for the stylesheet XSL=new StAXSource(eventReaderXSL); // for XML file XMLEventReader eventReaderXML=null; //define the Source object for the XML document XML=new StAXSource(eventReaderXML); // result fo transformation XMLStreamWriter streamWriter=null; //define the Result object Result XML_r=new StAXResult(streamWriter);









Email Blog Entry
sorry for all the questions
thanks...
06-14-2013, 02:22 PM in gbonecapone