Share this page 

Attach a stylesheet to an XML fileTag(s): XML


Consider the following XML file, a simple xsl is attached to nicely format the data.
Modern browsers (like N7 or IE5.5) support XML and XSL transformation.

bruce.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="bruce.xsl"?>
<data>
    <guynamedbruce>
        <FirstName>Bruce</FirstName>
        <LastName>Lee</LastName>
    </guynamedbruce>
    <guynamedbruce>
        <FirstName>Bruce</FirstName>
        <LastName>Willis</LastName>
    </guynamedbruce>
    <guynamedbruce>
        <FirstName>Bruce</FirstName>
        <LastName>Wayne</LastName>
    </guynamedbruce>
</data>

bruce.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="data/guynamedbruce">
<tr>
   <td>
     <xsl:value-of select="FirstName"/>
   </td>
   <td>
     <xsl:value-of select="LastName"/>
   </td>  
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Try it here