20
1 JAXP & XSLT JAXP & XSLT

1 JAXP & XSLT. Objectives 2 TrAX API Transforming XML Documents Workshops

Embed Size (px)

Citation preview

Page 1: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

1

JAXP & XSLT JAXP & XSLT

Page 2: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Objectives

2

TrAX APITransforming XML Documents Workshops

Page 3: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

TrAX API

XML Transformation Transforms the XML document into another XML document as

output, which fulfills some particular objective. There are two unique cases

XML to XML The output is always an XML document. It is mostly used when you need to create XML pipelines from XML chain.

The XML pipeline is created when XML transformations are connected to each other.

The XML chain is the collection of XML documents which bind with each other.

XML to Data The output is always a byte stream. Comes under this case because the HTML document contains a stream of

bytes

TrAX APIs Is an API, which provides a general transformation model for

converting XML documents

Page 4: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

TrAX API packages

Packages Descriptions

javax.xml.transform

- Implements the generic APIs for transformation instructions and executing the transformation, starting from

source to destination. - The interfaces present in this package are ErrorListener, Result, Source, SourceLocator, Templates, URIResolver.

- The classes present in this package are OutputKeys, Transformer, TransformerFactory

javax.xml.transform.stream- Implements streams and URI specific transformation APIs.

- The classes present in this package are StreamResult, StreamSource

javax.xml.transform.dom- Implements DOM-related transformation APIs.

- The interface present in this package is DOMLocator

javax.xml.transform.sax- Implements SAX-related transformation APIs.

- The interfaces present in this package are TemplateHandler, TransformerHandler

Page 5: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

XSLT Stylesheets

Is a language used for transforming XML documents into XHTML documents for Web pages.

Uses XPath language to navigate between XML documents and XHTML documents.

Defines the source documents, which will be matched with a predefined set of templates in transformation process. If XSLT gets the same document, it transforms the matching part of the source document into the output document.

XML

Book

title author price

XSLT

html

head body

h1 h2 p

text ...Serialization

<html><head><title>abc</title></head>...</html>

Page 6: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Transformer class

Is used to transform a XML source tree into a result tree that is a well formed xml output document Methods Descriptions

transform- public abstract void transform(Source xmlSource, Result

outputTarget) throws TranformerException - Is used to process the source tree into the result tree

setParameter- public abstract void setParameter(String name, Object value)

- Is used to add parameter for the transformation process. - The parameters help in transformation the document

getParameter- public abstract Object getParameter(String name)

- Is used to get a parameter, which explicitly set using the setParameter() or setParameters() method

clearParameters- public abstract void clearParameters()

- Is used to clear all parameters set with the setParameter method

setURIResolver- public abstract void setURIResolver(URIResolver resolver)

- Sets an object, which is used to resolve the URIs

getURIResolver- public abstract URIResolver getURIResolver()

- Gets an object, which is used to resolve the URIs

Page 7: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Transformer class (cont)

Methods Descriptions

setOutputProperties- public abstract void setOutputProperties(Properties prop) throws

IllegalArgumentException- Sets the output properties for transformation

getOutputProperties- public abstract Properties getOutputProperties()

- Gets a copy of the output properties for transformation.

setOutputProperty- public abstract void setOutputProperties(String name, String value)

throws IllegalArgumentException- Sets an output property, which makes effects for the transformation

getOutputProperty

- public abstract String getOutputProperties(String name) throws IllegalArgumentException

- Gets an output property, which makes effects for the transformation

setErrorListener- public abstract void setErrorListener(ErrorListener listener) throws

IllegalArgumentException- Sets the error event listener for the transformation

getErrorListener- public abstract ErrorListener getErrorListener()

- Gets the error event listener for the transformation

Page 8: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

TransformerFactory class

Methods Descriptions

newInstance- public static TranformerFactory newInstance() throws TransformerFactoryConfigurationError - Gets a new instance of a TransformerFactory

newTransformer

- public abstract Transformer newTransformer(Source source) throws TransformerConfigurationException- Is used to convert the source object into a Transformer object. This object should not be used in multiple threads

newTemplates

- public abstract Templates newTemplates(Source source) throws TransformerConfigurationException- Is used to convert the source object into a complied representation of source template

setURIResolver- public abstract void setURIResolver(URIResolver resolver)- Is used to set the object that is to be used by default throughout the transformation

getURIResolver- public abstract URIResolver getURIResolver()- Is used to get the object that is to be used by default throughout the transformation

Page 9: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

TransformerFactory class (cont)

Methods Descriptions

getFeature- public abstract Boolean getFeature(String name)

- Is used to search the value of a parameter

setAttribute- public abstract void setAttribute(String name, Object value) throws

IllegalArgumentException- Permits the user to set the values of specific attributes

getAttribute- public abstract Object getAttribute(String name) throws

IllegalArgumentException- Permits the user to get the values of specific attributes

setErrorListener- public abstract void setErrorListener(ErrorListener listener) throws

IllegalArgumentException- Is used to set the error event listener

getErrorListener- public abstract ErrorListener getErrorListener()

- Is used to get the error event listener

Page 10: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

OutputKeys

1. public static void main(String[] args) throws Exception {

2. TransformerFactory tf = transformerFactory.newInstance();

3. Transformer t = tf.newTransformer();

4. t.setOutputProperty(OutputKeys.INDENT, "yes");

5. t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

6. t.setOutputProperty(OutputKeys.METHOD, "xml");

7. Source source = new SAXSource(new SerializeSax(), new InputSource());

8. Result result = new StreamResult(new OutputStreamWriter(System.out));

9. t.transform(source, result);

10. } 10

Page 11: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Source & Result interface

Source Interface

Result Interface

Methods Descriptions

setSystemId- public void setSystemId(String systemId)

- Is used to set the system identifier for Source

getSystemId- public String getSystemId()

- Is used to get the system identifier, which previously set with setSystemId()

Methods Descriptions

setSystemId- public void setSystemId(String systemId)

- Is used to set the system identifier for Result class

getSystemId- public String getSystemId()

- Is used to get the system identifier, which previously set with setSystemId() method

Page 12: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Template interface

The object, which implements the Template interface, is the runtime result of transformation instructions.

Can be called from multiple threads for a particular instance.

Methods Descriptions

newTransformer- public Transformer newTransformer() throws

TransformerConfigurationException - Is used to create a new transformation object

getOutputProperties- public Properties getOutputProperties ()

- Is used to get the static properties for xsl:output. The xsl:output is used to define the format of the output

Page 13: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Transforming XML documents

Page 14: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

TransformerFactory

To create transformer and Template objects, an instance of the TransformerFactory class is used.

Features of TransformerFactory are The system property of TransformerFactory is always set as

javax.xml.transform. TransformerFactory. The default property is used by the TransformerFactory class if

the property is not set manually. The newInstance() method is used by TransformerFactory class

to create instance. A TransformerFactory never perform multiple operations at a

time A transformation always has a source and a result.

Page 15: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Transformer

An instance of transformer class is used to change a source tree into a result tree in an XML document. The TransformerFactory.newTransformer() method is used to get the instances of Transformer classTo process the XML data from other sources and write the outputs, the instance of Transformer class is used. The objects of Transformer class functions correctly during simultaneous execution by multiple threads. Some methods used by this class are clearParameters() and getURIResolver().

Page 16: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Example

public class TraxEx {public static void main (String[] args) {

if(args.length<=0){ System.out.println("Usage: java TraxEx xsltFile xmlFile SortedOrders.html"); return; } try { TransformerFactory tf = TransformerFactory.newInstance (); Transformer trans = tf.newTransformer (new StreamSource(args[0])); trans.transform (new StreamSource(args[1]), new StreamResult(new

FileOutputStream(args[2]))); System.out.println("File SortedOrders.html created in current folder"); }catch(Exception e){ e.printStackTrace (); } }}

Page 17: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Example (cont) XSLT file<?xml version="1.0" encoding="UTF-8" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/"> <TABLE BORDER="1"> <TR bgcolor="0x0000ff"> <TD><font color="#FFFFFF">Customer</font> </TD> <TD><font color="#FFFFFF"> Item </font></TD> <TD><font color="#FFFFFF"> Quantity </font></TD> <TD><font color="#FFFFFF">Price</font></TD> </TR> <xsl:apply-templates select ="customers"/></TABLE> </xsl:template> <xsl:template match="customers"> <xsl:apply-templates select ="customer"> <xsl:sort select="price" order="ascending" data-type="number"/> </xsl:apply-templates> </xsl:template> <xsl:template match="customer"> <TR> <TD> <font color="#0000ff" size="+1"> <xsl:value-of select ="name"/> </font></TD> <TD bgcolor="#00FFFF"><xsl:value-of select="item"/> </TD> <TD><p align="right"> <xsl:value-of select="quantity"/></p></TD> <TD bgcolor="#00FFFF"><p align="right"><xsl:value-of select="price"/></p></TD></TR> </xsl:template></xsl:stylesheet>

Page 18: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Example (cont) XML file<?xml version="1.0" encoding="UTF-8" ?><?xml-stylesheet type="text/xsl" href="OrderProcessing.xsl"?><customers>

<customer> <name>Chris</name> <item>Color TV</item> <quantity>5</quantity> <price>7500</price></customer><customer> <name>David</name> <item>DVD Player</item> <quantity>15</quantity> <price>4000</price></customer><customer> <name>Mitchell</name> <item>Washing Machine</item> <quantity>8</quantity> <price>10000</price></customer>

</customers>

Page 19: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

Example (cont)

Page 20: 1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops

WORKSHOP ACTIVITIES

Building the console Java application using XSLT to transform the XML document combining with XSLT file to HTML file.