JRR
JRR

Reputation: 317

xslt convert xml to html

I have 2 files, xml and xsl. I want to use xslt to take the xml data and convert to html table. Code below is not working in Chrome and I'm not sure why, any advice on how to use xslt properly AND/OR alternatives to xslt?

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style.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>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>`

style.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <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>`

Upvotes: 0

Views: 1434

Answers (2)

mt81
mt81

Reputation: 3318

You can use XSLT on the server side and serve HTML content directly, this way you won't have to worry about browser.

I already did it in java, but I am pretty sure you have XSLT engine in other languages too.

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116959

For one thing, your XSLT is missing the opening tag for the xsl:template instruction:

<xsl:template match="/">

For another, there is an inherent problem with running XSLT locally in Chrome - see: https://stackoverflow.com/a/3839054/3016153 (and others).

Upvotes: 1

Related Questions