XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (796 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
12.86Mb size Format: txt, pdf, ePub

You can specify values for global parameters defined in the stylesheet using a
keyword=value
notation; for example:

Transform -s:source.xml -xsl:style.xsl param1=value1 param2=value2

If the parameter names have a non-null namespace, you can use Clark notation for expanded names; for example,
{namespace-uri}local-name
. The parameter values are interpreted as strings. If the string contains a space, you should enclose it in quotes; for example,
param1=“John Brown”
. They are passed to the stylesheet as
xs:untypedAtomic
values, which means that if the

element declares a type such as
xs:integer
or
xs:string
, the value will be converted automatically to the required type, provided that the value is valid for that type.

If you want to pass an XML document as a parameter to the stylesheet, you can do this by prefixing the parameter name with
+
and supplying the name of the XML file as the parameter value. For example:

Transform -s:source.xml -xsl:style.xsl +lookup=lookup.xml

The XML contained in
lookup.xml
will be parsed, and the document node of the resulting tree will be passed to the stylesheet as the value of the stylesheet parameter named
lookup
.

You can also override

attributes using a similar notation, but prefixing the keyword with
!
. For example, to get indented output write:

Transform -s:source.xml -xsl:style.xsl !indent=yes

Using Saxon from a Java Application

When calling Saxon from Java, you have a choice between using the JAXP interface defined in Appendix E, or one of Saxon's native interfaces. Saxon 9.0 introduces a new Java interface called s9api (pronounced “snappy”) that is designed to eliminate some of the limitations and inconsistencies of the JAXP interfaces, but JAXP is still available if you need to keep your application portable. Although Saxon itself works with JDK 1.4, to use the s9api interface you will need Java 5 or later, because it takes advantage of features like the generic collection classes.

I will first describe how to use Saxon's implementation of the JAXP interfaces, and then take a look at the s9api design.

Using Saxon via JAXP Interfaces

JAXP interfaces are summarized in Appendix E, and you can find full specifications in the Sun Javadoc documentation: go to
http://java.sun.com/j2se/1.5.0/docs/api/index.html
, and look for the packages
javax.xml.transform
,
javax.xml.validation
, and
javax.xml.xpath
. These three packages allow you to:

  • Compile an XSLT stylesheet and run a transformation
  • Load a schema and validate a source document
  • Compile and run XPath expressions

These are described briefly in the following sections.

Running XSLT Transformations

The JAXP transformation API allows you to compile a stylesheet into a
Templates
object, which can then be used repeatedly (in series or in multiple threads) to process different source documents through the same stylesheet. This can greatly improve throughput on a Web server. Saxon supplies a sample application to achieve this, in the form of a Java servlet.

Saxon implements the whole of the
javax.xml.transform
package, including the
dom
,
sax
, and
stream
subpackages, both for input and output. It also implements the
SAXTransformerFactory,
which allows you to do the transformation as part of a SAX pipeline. If you want to use Saxon with the DOM you need to have
saxon9-dom.jar
on the classpath.

The implementation of
TransformerFactory
in Saxon-B is the class
net.sf.saxon.TransformerFactoryImpl
, and this is used in the examples below. The equivalent in Saxon-SA is
com.saxonica.SchemaAwareTransformerFactory
.

The
saxon9.jar
package includes a services file that has the effect of causing the JAXP
TransformerFactory
to choose Saxon as the default XSLT processor. However, it can be tricky to ensure that Saxon is loaded, given that the JDK includes an XSLT implementation (Xalan) as a standard component. The best policy, if you require Saxon because your stylesheet is written in XSLT 2.0, is to select it explicitly. There are several ways this can be achieved:

Other books

Ancestral Vices by Tom Sharpe
Ex Delicto by Valentina Khorkina
Bradley Wiggins by John Deering
Falling for Hamlet by Michelle Ray
Prairie Ostrich by Tamai Kobayashi
River of The Dead by Barbara Nadel
The Good Neighbor by William Kowalski