Accessing Java code via XSLT [turn]

Source: Internet
Author: User
Tags add date functions string version variable xmlns access
Access if you want to convert XML data to a fixed-width and plain-text string-This is just one of the features that XSLT does not easily implement, or you might want to perform word substitution in an XML file. In these cases, when your transaction exceeds the processing power of XSLT itself, you can do so by invoking the Java function in your XSLT processor.
I've seen examples of special processing using scripting languages like VBScript and JScript (for Microsoft parsers), but it's not particularly obvious how Java can be used to perform special processing. However, simply put, Java functions can be invoked in an XSLT processor through an XML namespace (namespaces). The combination of Java and XSLT provides a powerful way to build robust applications.
Use a custom namespace \ r
Namespaces are a way to modify the names of elements and attributes in an XSL by using a Uniform Resource Identifier (URI). For example, a common XSL element needs to be decorated with an XSL namespace prefix, which is declared by using the XSLT namespace URI:
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform"
But not limited to using the XSLT namespace, you are perfectly free to declare your own custom namespaces and use them to identify custom XSLT tags and scopes.
You must use a custom namespace prefix to perform a call to an external function, for example, Mynspace:fundtion (), the namespace prefix mynspace, must be associated with a namespace URI, for example:
Xmlns:myspace= "D:///xmlprogs/mynamespace"
A generic Java binding uses a namespace URI to identify the location of a Java class function. For the Xalan XSLT processor (version 1.2.2) for the sample code in this article, the namespace URI must have a uri-stem/packagename form. If you use a XT, your namespace should have the following format:
Xmlns:mynspace= "D://xmlprogs/mynamespace.myclassname
In this case, you can use the XSLT code to invoke Java functions from myClassName.
Binding external Java functions
Here is a procedure for binding external functions written in the Java language inside an XSLT template.
Add your own custom namespaces as their attributes to the stylesheet (stylesheet) element or to your template.
Make sure that your Java class files are located in the classpath of the Java Virtual Machine (JVM) where your XSLT processor is running.
For static methods, you can use a fully qualified class name to add a method and any required parameters, and of course, the namespace prefix.
For non-static methods, first create the class instance, and then call the method and provide any required parameters.
For example, the following style sheet element contains a custom namespace declaration:

<?xml version= "1.0" encoding= "UTF-8" ><xsl:stylesheet "version=" 1.0 "xmlns:xsl=" /transform "xmlns:myspace=" D://xmlprogs/mynamespace ">

After you declare this namespace, you can access any of the static methods of the class, like this:

<xsl:variable name= "Funvalue" select= "mynspace:classname.function ()"/>

If the method is not static, then the class constructor must be called first, for example:

<xsl:variable name= "Funvalue" select= "Mynspace.classname.new ()"/><?xml version= "1.0" encoding= "UTF-8"? ><xsl:stylesheet version= "1.0" xmlns:xsl= http://www.w3.org/1999/XSL/Transform "xmlns:java=" d://xmlprogs/ MyNamespace "Exclude-result-prefixes=" java >

The Java package \ "MyNamespace" must be located in the "D://xmlprogs" directory.
Note: The use of optional attribute xsl:exclude-result-prefixes prevents the Java namespace from being included in the resulting document.

<xsl:template match= "/" >
In the XSLT code above, <xsl:value-of select= "java:DateClass.displayDate ()"/> The row invokes the static method Displaydate () of the class that is named Dateclass in the MyNamespace package.
With this technique, you can invoke any Java class method-including those that require parameters-from your XSLT template.
The code involved in this article: date.xsl

<?xml version= "1.0" encoding= "UTF-8" ><xsl:stylesheet "version=" 1.0 "xmlns:xsl=" /transform "xmlns:java=" D://xmlprogs/mynamespace "exclude-result-prefixes=" java ><xsl:template match= "/" >
To run this code, you need:
Create the folder "D:xmlprogsmynamespace".
Put the Dateclass.java file into the above path and compile the Dateclass.class file.
Place "D:xmlprogsmynamespace" into the CLASSPATH variable.
Use an XSLT processor, such as XT or Xalan, to associate the XSL file with a specific XML file.

Related Article

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.