Powered by SmartDoc

xslt option

The xslt option specifies Relaxer's XSLT generator.

parameter

The xslt option takes no parameters. If you specify the xslt option, this means that you want to use the XSLT generator.

Artifacts

The xslt option generates an XSLT stylesheet from the resource specified by the parameter.

Example

List 33.2.3.1[xslt.rng] is a sample RELAX NG schema.

xslt.rng
<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>

Execution of Relaxer with the xslt options is as follows:

$ relaxer -xslt xslt.rng

As a result, Relaxer generates one file:

An artifact of the XSLT generator is shown in List 33.2.3.2[xslt.xsl].

xslt.xsl
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" \
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml"/>
  <xsl:template match="address">
    <address>
      <xsl:attribute name="zip">
        <xsl:value-of select="@zip"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </address>
  </xsl:template>
  <xsl:template match="balance">
    <balance>
      <xsl:apply-templates/>
    </balance>
  </xsl:template>
  <xsl:template match="account[balance and owner and address]">
    <account>
      <xsl:attribute name="accountNo">
        <xsl:value-of select="@accountNo"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </account>
  </xsl:template>
  <xsl:template match="owner">
    <owner>
      <xsl:apply-templates/>
    </owner>
  </xsl:template>
  <xsl:template match="phone">
    <phone>
      <xsl:attribute name="area">
        <xsl:value-of select="@area"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </phone>
  </xsl:template>
</xsl:stylesheet>

List 33.2.3.3[xslt.xml] is a sample XML document.

xslt.xml
<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>

List 33.2.3.4[Xslt.java] is a sample application that performs an XSLT transformation using JAXP's XSLT facilities, javax.xml.transform.* and javax.xml.transform.stream.*.

Xslt.java
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 Xslt {
    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);
    }
}

To run the sample application, enter the following command:

$ java Xslt xslt.xml xslt.xsl
<?xml version="1.0" encoding="UTF-8"?>
<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>

This is, in effect, an identity transform, so the same content that is in the original source document is displayed.