The xslt.template option specifies a separate XSLT template file.
The xslt.template option takes one parameter, which is a URL for a resource.
The xslt.template option does not produce immediate artifacts, but it affects the xslt option behavior.
To use the xslt.template option, in addition to a Relax Core or RELAX NG schema, an XSLT stylesheet is needed.
List 33.3.3.1[xsltTemplate.rng] is a sample RELAX NG schema.
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> <start> <ref name="account"/> </start> <define name="account"> <element name="account"> <attribute name="accountNo"> <data type="token"/> </attribute> <element name="balance"> <data type="int"/> </element> <element name="owner"> <data type="token"/> </element> <ref name="address"/> <zeroOrMore> <ref name="phone"/> </zeroOrMore> </element> </define> <define name="address"> <element name="address"> <attribute name="zip"> <data type="token"/> </attribute> <text/> </element> </define> <define name="phone"> <element name="phone"> <attribute name="area"> <data type="token"/> </attribute> <data type="token"/> </element> </define> </grammar>
List 33.3.3.2[template.xsl] is a sample template XSLT stylesheet.
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:label="http://www.relaxer.org/xmlns/relaxer/xslt/label" version="1.0"> <xsl:output indent="yes" method="xml"/> <xsl:template label:match="account"> <table> <thead> <tr> <th>Area</th> <th>Number</th> </tr> </thead> <tbody> <xsl:apply-templates label:select="phone"/> </tbody> </table> </xsl:template> <xsl:template label:match="phone"> <tr> <td><xsl:value-of select="@area"/></td> <td><xsl:apply-templates/></td> </tr> </xsl:template> </xsl:stylesheet>
$ relaxer -xslt -xslt.template:template.xsl xsltTemplate.rng
As a result of this command, Relaxer generates xsltTemplate.xsl as shown in List 33.3.3.3[xsltTemplate.xsl].
<?xml version='1.0'?> <xsl:stylesheet version="1.0" \ xmlns:label="http://www.relaxer.org/xmlns/relaxer/xslt/label" \ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes" method="xml"/> <xsl:template label:match="account" match="account[balance and owner and \ address]"> <table> <thead> <tr> <th>Area</th> <th>Number</th> </tr> </thead> <tbody> <xsl:apply-templates label:select="phone" select="phone"/> </tbody> </table> </xsl:template> <xsl:template label:match="phone" match="phone"> <tr> <td> <xsl:value-of select="@area"/> </td> <td> <xsl:apply-templates/> </td> </tr> </xsl:template> </xsl:stylesheet>
List 33.3.3.4[XsltTemplate.java] is a sample application to execute XSLT transformation using JAXP facility.
import java.io.File; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.Source; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; public class XsltTemplate { public static void main(String[] args) throws TransformerConfigurationException, TransformerException { File inputFile = new File(args[0]); File xsltFile = new File(args[1]); Source inputSource = new StreamSource(inputFile); Source xsltSource = new StreamSource(xsltFile); Result outputResult = new StreamResult(System.out); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(xsltSource); transformer.transform(inputSource, outputResult); } }
List 33.3.3.5[xsltTemplate.xml] is a sample XML document.
<account accountNo="12345"> <balance>102030</balance> <owner>XML Taro</owner> <address zip="213">Yokohama</address> <phone area="123">456-7890</phone> <phone area="090">123-4567</phone> </account>
To execute the sample application, do the following:
$ java XsltTemplate xsltTemplate.xml xsltTemplate.xsl <?xml version="1.0" encoding="UTF-8"?> <table \ xmlns:label="http://www.relaxer.org/xmlns/relaxer/xslt/label"> <thead> <tr> <th>Area</th> <th>Number</th> </tr> </thead> <tbody> <tr> <td>123</td> <td>456-7890</td> </tr> <tr> <td>090</td> <td>123-4567</td> </tr> </tbody> </table>