|
9.4. Transforming XML Using XSLTThe xslt task, also called the style task (the two names are interchangeable in Ant), can process a set of documents via XSLT. This is handy for building nicely formatted views of XML-based documentation in other formats like HTML, or for generating code. How do you use this task to perform XSLT transformations? Example 9-5 shows a build file that puts it to work, transforming style.xml into style.html, using style.xsl. Example 9-5. Using the xslt/style task ch09/xslt/build.xml<?xml version="1.0" encoding="UTF-8" ?> <project default="main" > <target name="main"> <xslt basedir="." destdir="." extension=".html" includes="style.xml" style="style.xsl"/> </target> </project> Here's style.xml, the XML document to transform, which holds data about three U.S. states: <?xml version="1.0" encoding ="UTF-8"?> <states> <state> <name>California</name> <population units="people">33871648</population><!--2000 census--> <capital>Sacramento</capital> <bird>Quail</bird> <flower>Golden Poppy</flower> <area units="square miles">155959</area> </state> <state> <name>Massachusetts</name> <population units="people">6349097</population><!--2000 census--> <capital>Boston</capital> <bird>Chickadee</bird> <flower>Mayflower</flower> <area units="square miles">7840</area> </state> <state> <name>New York</name> <population units="people">18976457</population><!--2000 census--> <capital>Albany</capital> <bird>Bluebird</bird> <flower>Rose</flower> <area units="square miles">47214</area> </state> </states> In this example, the names of the states are extracted and used to create an HTML document with style.xsl: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="states"> <HTML> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="state"> <P> <xsl:value-of select="name"/> </P> </xsl:template> </xsl:stylesheet> Here's the resulting HTML document: <HTML> <BODY> <P>California</P> <P>Massachusetts</P> <P>New York</P> </BODY> </HTML> The attributes of this task appear in Table 9-6.
This task forms an implicit fileset and so supports all attributes of fileset (dir becomes basedir) as well as the nested include, exclude, and patternset elements. You can use nested classpath elements to load the XSLT processor, nested xmlcatalog elements (see Table 9-3) for entity and URI resolution, and attribute elements (see Table 9-4) to specify settings of the processor factory (the attribute names and values are processor specific). The xslt/style task supports the use of nested param elements, whose attributes appear in Table 9-7, to pass values to xsl:param declarations in stylesheets.
You can use outputproperty elements, whose attributes appear in Table 9-8, with a TraX processor to specify how the result tree should be output.
TraX processors can accept factory elements to specify factory settings. These elements can contain one attribute, name, which specifies the fully qualified classname of the transformer factory to use. |
|