1 / 25

JAXP Transformation Package and Xalan Extensions

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.

nellie
Télécharger la présentation

JAXP Transformation Package and Xalan Extensions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAXP Transformation Package and Xalan Extensions 黃立昇 91753034

  2. Outline • JAXP Transformation Package • Xalan Extensions

  3. JAXP Transformation Package

  4. Packages • javax.xml.transform • javax.xml.transform.dom • javax.xml.transform.sax • javax.xml.transform.stream

  5. 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

  6. 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

  7. 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}

  8. 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}

  9. 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)

  10. Xalan Extensions

  11. 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

  12. 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

  13. 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

  14. 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

  15. JavaScript NetRexx BML JPython Jacl JScript VBScript PerlScript Support Languages • Direct supported by java • Use BFS (Bean Scripting Framework)

  16. 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>

  17. 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

  18. 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

  19. 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

  20. 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

  21. Xalan Extension Library • Support EXSLT standard extension library • Redirect, nodeset, NodeInfo, SQL, PipeDocument, evaluate, tokenize

  22. Redirect • Can used to redirect portions of your transformation output to multiple files • Supply three extension elements • <open> • <write> • <close>

  23. 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 >

  24. 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>

  25. 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)

More Related