Calling Java Directly from XSLT Processors

Calling Java Directly from XSLT Processors

As discussed in Chapter 5, until recently, XSLT processors have been free to define the way they implement extension functions, and one of those ways includes calling Java functions directly. For example, in Saxon and Xalan, you can run Java code if you define a namespace that specifies a Java class as the final part of its URI as follows , where Im defining a Date namespace that corresponds to the Java Date class:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.1"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:Date="http://www.saxon.com/java/java.util.Date">          .          .          . 

As you saw in Chapter 5, this means that you can use Java Date functions such as toString and new to embed the current date in an <H1> HTML header in the output this way:

Listing 10.6 Using the Java Date Function
 <?xml version="1.0"?>  <xsl:stylesheet version="1.1"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:Date="http://www.saxon.com/java/java.util.Date">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <BR/>                  <H1>                      <xsl:value-of select="Date:toString(Date:new())"/>                  </H1>                  <TABLE BORDER="2">                      <TD>Name</TD>                      <TD>Mass</TD>                      <TD>Radius</TD>                      <TD>Day</TD>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>      <xsl:template match="MASS">          <xsl:value-of select="."/>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="."/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="."/>      </xsl:template>  </xsl:stylesheet> 

This certainly works, but its a limited way to do things, and it relies on non-standard extensions. Unless youre doing only a few simple calls, its usually far better to start off with Java and interface to the XSLT processor instead.

The Xalan, Saxon, XT, and Oracle XSLT processors all define an API that you can use to call them from Java. All you have to do is make sure the correct JAR files are in the Java classpath . Youve already seen how to use JAR files and classpaths from as far back as Chapter 1; now its time to start writing some Java, not just running predefined classes from the command line. The code is available for download at http://www.newriders.com/books/title.cfm?isbn=0735711364, so if you are not a Java programmer you can follow along.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net