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?
- <%@ page contenttype= "text/html; charset="UTF-8 " language="java " errorpage=" "%> /c10>
- <%@ page import="javax.xml.transform.*, javax.xml.transform.stream.*"%>
- <%
- Create a conversion factory
- Transformerfactory tfactory = transformerfactory.newinstance ();
- Creates a transformer with the specified XSLT style sheet file
- Transformer Transformer = Tfactory.newtransformer (new Streamsource (Application.getrealpath ("web-inf/") + "/ Name.xslt "));
- Performs the conversion and outputs the converted target document as a response
- Transformer.transform (New Streamsource (Application.getrealpath ("web-inf/") + "/name.xml"), New Streamresult ( Response.getoutputstream ()));
- %>
<%@ 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)