Using XML + XSLT to include content

XSL is a language that can transform XML into XHTML, a language that can filter and sort XML data, a language that can define parts of a XML document, a language that can format XML data based on the data value, like displaying negative numbers in red, and a language that can output XML data to different devices, like screen, paper, or voice.

Part 1: Start with your XML Document

We want to transform the following XML document ("cdcatalog.xml") into XHTML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Happy Birthday</title>
<artist>Weird Al Yankovic</artist>
<country>USA</country>
<company>Scotti Brothers</company>
<price>0.99</price>
<year>1983</year>
</cd>
</catalog>

Part 2: Create an XSL Style Sheet 

Now we can create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:

<?xml   version="1.0"   encoding="ISO-8859-1"?>
<xsl:stylesheet   version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template  match="/"> <html> <body> <h2>My CD Collection</h2> <table> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

Part 3: Link the XSL Style Sheet to the XML Document

Finally, add an XSL Style Sheet reference to your XML document ("cdcatalog.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Happy Birthday</title>
<artist>Weird Al Yankovic</artist>
<country>USA</country>
<company>Scotti Brothers</company>
<price>0.99</price>
<year>1983</year>
</cd>
</catalog>

If you have an XSL compliant browser it will transform your XML into XHTML.

Internet Explorer and Firefox should render the XML into XHTML locally with no issues; with Chrome and Safari, you will need to upload the both the XML data and the XSL to a web server to view the formatted XML.