250 likes | 360 Vues
JAXP Transformation Package and Xalan Extensions. 黃立昇 91753034. Outline. JAXP Transformation Package Xalan Extensions. JAXP Transformation Package. Packages. javax.xml.transform javax.xml.transform.dom javax.xml.transform.sax javax.xml.transform.stream. javax.xml.transform.
E N D
JAXP Transformation Package and Xalan Extensions 黃立昇 91753034
Outline • JAXP Transformation Package • Xalan Extensions
Packages • javax.xml.transform • javax.xml.transform.dom • javax.xml.transform.sax • javax.xml.transform.stream
javax.xml.transform • Define the generic APIs for processing transformation instructions, and performing a transformation from source to result • Define the factory class to get a Transformer object • We then configure the transformer with the source and result, and invoke the transform() method to do the transformation
javax.xml.transform.* • javax.xml.transform.dom; javax.xml.transform.sax; javax.xml.transform.stream • Define the source and result class that let a dom tree/sax event or I/O stream as an input or output from a transformation
Example-1 (XML -> Stream) Transformer transformer; TransformerFactory factory = TransformerFactory.newInstance(); String stylesheet = "file:///home/user/mystylesheet.xsl"; String sourceId = "file:///home/user/sourcefile.xml"; try { transformer = factory.newTransformer( new StreamSource(stylesheet)); transformer.transform(new StreamSource(sourceId), new StreamResult(System.out)); } catch (Exception e) {// handle error}
Example-2 (DOM -> Stream) TransformerFactory tfactory = TransformerFactory.newInstance(); try{ Transformer serializer = tfactory.newTransformer(); Properties oprops = new Properties(); oprops.put(“method”, “html”); oprops.put(“indent-amount”, “2”); serializer.setOutputProperties(oprops); serializer.transform(new DOMSource(doc), new StreamResult(System.out)); } catch (Exception e) {// handle error}
How System Find the TransformerFactory Class • Use the javax.xml.transform.TransformerFactory system property • Use the properties file “%JAVA_HOME%/lib/jaxp.properties" in the JRE directory. • Look for the classname in the file META-INF/services/javax.xml.transform.TransformerFactory in jars available to the runtime. • Platform default TransformFactory instance. (org.apache.xalan.processor.TransformerFactoryImpl)
Xalan Introduction • Xalan is a XSLT processor provided by APACHE • It full implements XSLT 1.0 and XPATH 1.0 • Include an Interpretive processor (Xalan) for tools and a Compiling processor (XSLTC) for high performance runtime environment • JAXP Transformation default Transformator
XSLT Extensions • Specified in XSLT 1.0 Recommendation • Add the functionally of XSLT to call a procedural language • Allows two kinds of extension • extension elements • extension function • Does not provide a mechanism for define implementations of extension
Extension Elements • Extension element perform a action not transfers to the result tree • Declare some namespace as extension namespace, elements in the namespace are recognized extension elements • Using an extension-element-prefixes attribute on an xsl:stylesheet element • an extension-element-prefixes attribute on an literal result element • Can contain attributes, text nodes, other elements or any valid XML
Extension Functions • Can think extension functions as extended core library that XPATH provide • If the FunctionName in a FunctionCall expression in not an NCName (i.e. if it contains a colon ),then it is treated as a call to an extension function • The FunctionName is expanded to a name using the namespace declarations from the evaluation context • Pass arguments and returns a value
JavaScript NetRexx BML JPython Jacl JScript VBScript PerlScript Support Languages • Direct supported by java • Use BFS (Bean Scripting Framework)
A Simple Examplehttp://www.cs.nccu.edu.tw/~g9134/XML/simple_example.xslt • The source element contains a numdays attribute (<deadline numdays=“3”/>) • The extension element contains a multiplier attribute to set variable in the extension • The extension function compute the deadline (current date+numdays*multipler) • The result transformation of the deadline element will be: <p>We have logged your enquiry and will respond by April 29, 2000 12:07:16 PM EST.</p>
Syntax • Declare the xalan namespace • Declare a unique namespace for each extension prefix • If using extension elements, designate the extension element prefixe • (Optional) Exclude the extension namespace declaration from the result tree • exclude-result-prefixes="prefix-1 prefix-2 ...“ for element • xsl:exclude-result-prefixes="prefix-1 prefix-2 ...“ for literal result • Set up an xalan:component • Set up an xalan:script element
Extension Element Implementation Type element (org.apache.xalan.extensions.XSLProcessorContext, org.apache.xalan.templates.ElemExtensionCall extensionElement) • XSLProcessorContext provides access to the XSL processor,the XML source tree,the stylesheet tree,the current context node, and current mode • ElemExtensionCall provide the API for interacting with the extension element • The value returned by an extension element is placed in the transform result • If not interested in the return value, return null instead
Extension functions • Can be think as a extension of XPATH core function • May include arguments of any type and return a value of any type • If using java, Xalan-Java will automatically mapping xslt data typesto a corresponding java type(class) when passing arguments and return values (Node-set, String, Boolean, Number, Result Tree Fragment) • Any non-XSLT type is passed without conversion
Extension element and function provide a powerful mechanism for extending and simplifying what we can do with XSLT processor EXSLT provide a set of standard extension functions and elements to XSLT users Commom, math, set, date-and-time, dynamic, string Extensions Library
Xalan Extension Library • Support EXSLT standard extension library • Redirect, nodeset, NodeInfo, SQL, PipeDocument, evaluate, tokenize
Redirect • Can used to redirect portions of your transformation output to multiple files • Supply three extension elements • <open> • <write> • <close>
Examplehttp://plum.cs.nccu.edu.tw/~g9134/XML/redirect.xslt • Source document <?xml version="1.0"?> <doc> <foo file="foo.out"> Testing Redirect extension: <bar>A foo subelement text node</bar> </foo> <main> Everything else </main> </doc >
The standard output <?xml version="1.0" encoding="UTF-8"?> <standard-out>Standard output: <main> Everything else. </main> <standard-out> • The output redirected to foo.out <?xml version="1.0" encoding="UTF-8"?> <foo-out> Testing Redirect extension: <foobar-out>foo subelement text node</foobar-out> </foo-out>
Reference • JAXP spec. (http://java.sun.com/) • The java web service tutorial (http://java.sun.com/) • Xalan-Java (http://xml.apache.org/xalan-j/index.html) • EXSLT (http://exslt.org)