Java Integration
Allows method invocation upon existing Java classes and objects (that is invocation of static and instance methods) and object creation (constructor invocation) upon existing classes.
To implement an extension function in Java, stylesheet developers associate an implementation to their extension function namespace prefix using xsl:script element with language=”java” as in the following example:
<?xml
version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:date="uri.any">
<xsl:script implements-prefix="date" language="java"
src="java:com.example.datestuff.DateRoutines"/>
<xsl:template match="/">
<OrderDate>
<xsl:value-of select="date:format(/order/date,'MM/DD/YY')"/>
</OrderDate>
</xsl:template>
</xsl:stylesheet>
For Java, the value of the src attribute on xsl:script is a URI reference identifying a Java class. The methods of this class implement the functions, which expanded names, have the namespace URI specified by the implements-prefix attribute. The URI specified by the src attribute may use the java: URI scheme: src="java:fully.qualified.ClassName"; the XSLT processor may also allow the URI to use other URI schemes to locate a resource containing the Java class file.
Here is an example XSL document that uses Java Language Binding:
<?xml
version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myobj="com.sap.engine.lib.xml.parser.Myobj">
<xsl:param name="myobj"/>
<xsl:template match = "/">
At this
moment:<xsl:value-of select="string(myobj:getToday())"/>
we offer:
<xsl:apply-templates/>
<xsl:value-of
select="myobj:callNodeList($myobj, /)"/>
</xsl:template>
</xsl:stylesheet>
Applying the latter to the following XML document:
<?xml
version="1.0"?>
<grocery name = "The red tomato">
<fruit>
<article>
Apples</article>
<article>
Pears </article>
<article>
Bananas </article>
</fruit>
<vegetables>
<article>Cucumbers </article>
<article>
Tomatoes </article>
<article> Peppers
</article>
</vegetables>
</grocery>
Results in:
<?xml version='1.0'
encoding='utf-8'?>
At this moment: Wed Oct 17
17:48:53 EEST 2001
we offer:
Apples
Pears
Bananas
Cucumbers
Tomatoes
Peppers
To run this example, a parameter should be added to the transformer, prior to transformation in the following way:
Myobj my = new
Myobj();
transformer.setParameter("myobj", my);
Where the class definition of the class Myobj looks like:
public class Myobj {
public void callNodeList(Node n) {
System.out.println(n);
}
public static java.util.Date getToday() {
java.util.Calendar cal =
ava.util.Calendar.getInstance();
return cal.getTime();
}
}