Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAXP Transformation Package and Xalan Extensions 黃立昇 91753034.

Similar presentations


Presentation on theme: "JAXP Transformation Package and Xalan Extensions 黃立昇 91753034."— 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 Support Languages JavaScript NetRexx BML JPython Jacl JScript VBScript PerlScript Direct supported by java Use BFS (Bean Scripting Framework)

16 A Simple Example http://www.cs.nccu.edu.tw/~g9134/XML/simple_example.xslt http://www.cs.nccu.edu.tw/~g9134/XML/simple_example.xslt The source element contains a numdays attribute ( ) 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: We have logged your enquiry and will respond by April 29, 2000 12:07:16 PM EST.

17 Syntax 1. Declare the xalan namespace 2. Declare a unique namespace for each extension prefix 3. If using extension elements, designate the extension element prefixe 4. (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 5. Set up an xalan:component 6. 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 Extensions Library 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

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 

23 Example http://plum.cs.nccu.edu.tw/~g9134/XML/redirect.xslt http://plum.cs.nccu.edu.tw/~g9134/XML/redirect.xslt Source document Testing Redirect extension: A foo subelement text node Everything else

24 The output redirected to foo.out Testing Redirect extension: foo subelement text node The standard output Standard output: Everything else.

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)


Download ppt "JAXP Transformation Package and Xalan Extensions 黃立昇 91753034."

Similar presentations


Ads by Google