Transforming XML Using XSLT

The 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> ...

Get Ant: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.