Here's a simple example that uses XSLT to convert data in an XML file into a table in an HTML file. You can use it to experiment with simple XSLT transforms.
Prerequisite: Install a Java Runtime Environment and add the location of the JRE to your PATH variable. (On Windows, most installers will add Java to your path for you.) If this works, you should be able to open a command-line window and run the command java -version
and get a printout of info about your JRE.
pets.xml
with the following code:<pets> <petType name="Dogs"> <pet id="123" name="Sparky" vaccineStatus="vaccinated" healthStatus="healthy"/> <pet id="234" name="Sadie" vaccineStatus="unvaccinated" healthStatus="sick"/> <pet id="345" name="Herman" vaccineStatus="unvaccinated" healthStatus="unknown"/> </petType> <petType name="Cats"> <pet id="456" name="Cleo" vaccineStatus="vaccinated" healthStatus="healthy"/> <pet id="567" name="Janet" vaccineStatus="unvaccinated" healthStatus="healthy"/> <pet id="678" name="Furry" vaccineStatus="vaccinated" healthStatus="sick"/> </petType> </pets>
petTransform.xsl
with the following code:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <!-- handle the root XML element --> <xsl:template match="/"> <html><head> <title>Pets that are available for adoption</title> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="pets"> <xsl:apply-templates/> </xsl:template> <xsl:template match="petType"> <h2><xsl:value-of select="@name"/></h2> <table id="{@name}"> <tr> <th colname="id">ID</th> <th colname="name">Name</th> <th colname="vaccinated">Vaccine status</th> <th colname="health">Health status</th> </tr> <tbody> <!-- add a row for each pet in this category --> <xsl:for-each select="pet"> <tr> <td colname="id"><xsl:value-of select="@id"/></td> <td colname="name"><xsl:value-of select="@name"/></td> <td colname="vaccinated"><xsl:value-of select="@vaccineStatus"/></td> <td colname="health"><xsl:value-of select="@healthStatus"/></td> </tr> </xsl:for-each> </tbody> </table> </xsl:template> <!-- ignore the content of other tags because we processed them elsewhere --> <xsl:template match="*"> <!-- do nothing --> </xsl:template> </xsl:stylesheet>
path_to_saxon.jar
is the full path to the file saxon9he.jar
:java -jar "path_to_saxon.jar" -o
petOutput.html -s:pets.xml -xsl:pettransform.xslt
For example:
java -jar "C:\Program Files\SaxonHE9-7-0-7J\saxon9he.jar" -o
petOutput.html -s:pets.xml -xsl:pettransform.xslt
Make sure to run this command on a single line.
petOutput.html
in a text editor. It should look like this:<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Pets that are available for adoption</title>
</head>
<body>
<h2>Dogs</h2>
<table id="Dogs">
<tr>
<th colname="id">ID</th>
<th colname="name">Name</th>
<th colname="vaccinated">Vaccine status</th>
<th colname="health">Health status</th>
</tr>
<tbody>
<tr>
<td colname="id">123</td>
<td colname="name">Sparky</td>
<td colname="vaccinated">vaccinated</td>
<td colname="health">healthy</td>
</tr>
<tr>
<td colname="id">234</td>
<td colname="name">Sadie</td>
<td colname="vaccinated">unvaccinated</td>
<td colname="health">sick</td>
</tr>
<tr>
<td colname="id">345</td>
<td colname="name">Herman</td>
<td colname="vaccinated">unvaccinated</td>
<td colname="health">unknown</td>
</tr>
</tbody>
</table>
<h2>Cats</h2>
<table id="Cats">
<tr>
<th colname="id">ID</th>
<th colname="name">Name</th>
<th colname="vaccinated">Vaccine status</th>
<th colname="health">Health status</th>
</tr>
<tbody>
<tr>
<td colname="id">456</td>
<td colname="name">Cleo</td>
<td colname="vaccinated">vaccinated</td>
<td colname="health">healthy</td>
</tr>
<tr>
<td colname="id">567</td>
<td colname="name">Janet</td>
<td colname="vaccinated">unvaccinated</td>
<td colname="health">healthy</td>
</tr>
<tr>
<td colname="id">678</td>
<td colname="name">Furry</td>
<td colname="vaccinated">vaccinated</td>
<td colname="health">sick</td>
</tr>
</tbody>
</table>
</body>
</html>
petOutput.html
in a web browser. It should show the data in a simple table.