Learn notes on the use of--XSLT converters (Xalan and Saxon).

Source: Internet
Author: User
Tags xsl xslt

Transferred from: http://blog.csdn.net/crystalbruce/article/details/7401602

XSLT is divided into two categories:

1: Client-side conversion: Requires browser support.

2: Server conversion: You need to use programming to convert XML to the appropriate HTML from the XSLT document, in the output to the client.

Server conversions fall into two categories:

1: Real-time conversion: When the server receives a client request, the XML is temporarily transformed from the XSLT using a dynamic scripting language such as JSP, PHP, and then sent to the client after the converted HTML.

2: Batch conversion: The server periodically transforms the XML, and when the server receives the request, it sends the existing HTML directly to the client, which is suitable for situations where the XML changes less frequently.

Common XSLT Conversion processors:

The 1:xalan:apache of the Child project. Website http://xalan.apache.org

The 2:saxon:sourceforge of the Child project. Website Http://saxon.sourceforge.net

Xalan processor

Required Packages:

The core class library of Xalan.jar:Xalan needs to rely on Serializer.jar.

Xercesimpl.jar, Xml-apis.jar:java API for XML processing (JAXP) core class library.

How to use:

The Xalan.jar itself is an executable program.

One: Use in DOS

Input

Java-classpath Serializer.jar-jar Xalan.jar

command outputs various options for Xalan.jar, the following are common options:

-in: Specifies the XML to be converted;

-xsl: Specifies the XSLT style sheet used, which can be omitted if XSLT has been introduced in the XML document;

-out: Specifies the target document for the output after conversion.

Example:

Java-classpath Serializer.jar-jar xalan.jar-in name.xml-out.name.html

Two: Using the programming method in the Web application

The main classes and interfaces used are as follows:

1:transformerfactory: Converter factory, responsible for the production of converters;

The 2:TRANSFORMER:XSLT converter is responsible for loading the XSLT style sheet document and performing the conversion;

3:source: The interface representing the source XML document, its common implementation class has Domsource, Streamsource, SAXSource;

4:result: The document interface representing the result of conversion, and its common implementation classes are Domresult, Streamresult, SAXResult.

Steps:

1: Create a converter factory using Transformerfactory's Newinstance () method;

2: Call the Converter Factory's Newtransformer (Source Xmlsource) method to create a converter;

3: Call the converter's transform (Source xmlsource, Result Outputtarget) method to perform the conversion.

Examples of programs:

[HTML]View Plaincopyprint?
  1. <%@ page contenttype= "text/html; charset="UTF-8 " language="java " errorpage=" "%> /c10>
  2. <%@ page import="javax.xml.transform.*, javax.xml.transform.stream.*"%>
  3. <%
  4. Create a conversion factory
  5. Transformerfactory tfactory = transformerfactory.newinstance ();
  6. Creates a transformer with the specified XSLT style sheet file
  7. Transformer Transformer = Tfactory.newtransformer (new Streamsource (Application.getrealpath ("web-inf/") + "/  Name.xslt "));
  8. Performs the conversion and outputs the converted target document as a response
  9. Transformer.transform (New Streamsource (Application.getrealpath ("web-inf/") + "/name.xml"), New Streamresult ( Response.getoutputstream ()));
  10. %>
<%@ page contenttype= "text/html; charset= "UTF-8" language= "java" errorpage= "%><%@ page import=" javax.xml.transform.*, javax.xml.transform.stream.* "%><%//Create a conversion factory transformerfactory tfactory = Transformerfactory.newinstance (); /Create a converter with the specified XSLT style sheet file transformer transformer = Tfactory.newtransformer (New Streamsource (Application.getrealpath (" web-inf/") +"/name.xslt ");//performs the conversion and converts the converted target document as the response output transformer.transform (new Streamsource (Application.getrealpath ("web-inf/") + "/name.xml"), New Streamresult (Response.getoutputstream ()));%>
Saxon processor

Saxon is a powerful feature that implements XSLT3.0, XQuery3.0, and XPath3.0 specifications.

Saxon9.jar is similar to this name as the core class library.

They are used in a similar way to Xalan.

Use the command:

Java-jar saxon9.jar-s:xmlsource.xml-o:outputtarget.html Xsl:name.xslt

The programming method is the same as the Xalan programming method, because all are oriented to the JAXP specification programming

Learn notes on the use of--XSLT converters (Xalan and Saxon). (EXT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.