1. Current mainstream Transformer implementation
Java sdk requires that the XSLT transformer implement the abstract methods in the javax. xml. transform. TransformerFactory abstract class. This factory class becomes the entry for creating a specific Transformer instance. Currently, the mainstream implementations are:
Saxon 6.x: com. icl. saxon. TransformerFactoryImpl
Saxon 7.x: net. sf. saxon. TransformerFactoryImpl
Xalan: org. apache. xalan. processor. TransformerFactoryImpl
Jd. xslt: jd. xml. xslt. trax. TransformerFactoryImpl
Oracle: oracle. xml. jaxp. JXSAXTransformerFactory
In addition, open the javax. xml. transform. transformerFactory. after reading the java file, look at the finder function and you will find that j2se calls com.sun.org. apache. xalan. internal. TC. trax. transformerFactoryImpl.
On the api web page mentioned above, we introduced how to use these portals. You can perform the following steps:
1. Call System. setProperty ("javax. xml. transform. TransformerFactory", "classname ")
2. Set the command line parameter-Djavax. xml. transform. TransformerFactory = classname.
3. Go to the JRE path to modify the lib/jaxp. properties file and add the following line of text
Javax. xml. parsers. DocumentBuilderFacotry = classname
4. Name a file named javax. xml. transform. TransformerFactory in the META-INF/services/directory. Edit this file and configure the required Transformer package
5. In the end, you can use the Transformer that comes with jdk. From Sun's JDK1.4, jdk's built-in Transformer is Xalan. JDK1.4 is Xalan2.2d10.
2 Use of Transformer
1. Create the StreamSource of the Upload File. The StreamSource class encapsulates an xsl file/data object into a data source that provides the conversion rules to Transformer. Tansformer can be used to instantiate a conversion processing entry.
2. Construct input and input objects. The input and output objects are instances of the StreamResult class.
3. Call Transformer's transform method to convert data.
The sample code is as follows:
TransformerFactoryfactory = TransformerFactory. newInstance ();
StreamResultinput = new StreamResult (xmlReader );
StreamResultresult = new StreamResult (outWriter );
Factory. newTransformer (newStreamSource (writable file). transform (input, result );
TransformerFactory
StreamSource
<Xml Source>
Transformer
<Object>
StreamResult
<Input>
StreamResult
<Result>
3 Java extension of xslt
Currently, I have only studied the xslt extension of the original java interface. I did not perform any Test Research on other Transformer processor java extension methods.
3.1 extend xslt through Java sdk
Let's take a look at the following code:
<Xsl: templatename = "nowHour" xmlns: date = "java. util. Date">
<Xsl: variablename = "now" select = "date: new ()"/>
<Xsl: value-ofselect = "now: getHours ($ now)"/>
</Xsl: template>
We can see that when defining a template, we can introduce the java. util. Date class of java into the namespace, bind it to an xmlns, and name it date. Then, define the variable, instantiate the Date, and bind it to the variable now. Finally, output the current hour in the format of now: getHours ($ now. This includes the following usage modes:
1. Use xmlns to bind the class with the namespace xmlns: xxx = "xxx. xxx. xxxx ";
2. Call the class initialization function: xxx: new (). Xxx is the namespace bound to a specific class in 1.
3. Call methods in the class: xxx: mmm ($ ppp ,....);
A) xxx is the namespace name bound to a specific java class. (Same as the form in 2 .)
B) if the Second Part binds the variable to the instantiated object of a class. Ppp is the name of this variable. This is the same as using xslt variables.
C) $ ppp is the parameter sequence provided to the method when it is called.
When using the above development mode, java. util. Date can be replaced with your own class to implement the functions you want to achieve.