1

I have got this strange problem.XSL is not working with xml.This is my xml and xsl code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xsl-stylesheet type= "text/xsl" href= "w3c.xsl"?>
<catalog>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd>
        <title>Eros</title>
        <artist>Eros Ramazzotti</artist>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
    <cd>
        <title>Romanza</title>
        <artist>Andrea Bocelli</artist>
        <country>EU</country>
        <company>Polydor</company>
        <price>10.80</price>
        <year>1996</year>
    </cd>
</catalog>

This is my xsl:

<?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 xmlns="http://www.w3.org/1999/xhtml">
  <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>

Note:

I took this code from W3C and hence it's a working one.I have googled a lot but couldn't find any working solution.I have used ie,firefox and chrome doesn't work in any of them.I have tested it in my remote server doesn't work either.The error I get is "This XML file does not appear to have any style information associated with it. The document tree is shown below.".The same error occurs.In the href I have tried all possible links from full path to just file name.I have also tried chrome with --allow-file-access-from-files but doesn't work.Please help me fix this.

1

2 Answers 2

3

Note that the second line of your XML file says:

<?xsl-stylesheet type= "text/xsl" href= "w3c.xls"?>

Notice the w3c.xls (instead of xsl ?) what are your filenames? And notice that is is xml-stylesheet, not xsl-stylesheet.

So this should do it:

<?xml-stylesheet type= "text/xsl" href= "w3c.xsl"?>
2
  • Yep. Missing the reference to the stylesheet.
    – feathj
    Aug 28, 2012 at 13:42
  • It doesn't work.That was a typing mistake. Aug 28, 2012 at 13:43
0

I cant reproduce this problem.

When the provided XSLT stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <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>

is applied (with all 9 different XSLT processors I have available) to the provided XML document:

<catalog>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd>
        <title>Eros</title>
        <artist>Eros Ramazzotti</artist>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
    <cd>
        <title>Romanza</title>
        <artist>Andrea Bocelli</artist>
        <country>EU</country>
        <company>Polydor</company>
        <price>10.80</price>
        <year>1996</year>
    </cd>
</catalog>

a correct result is produced:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <h2>My CD Collection</h2>
        <table border="1">
            <tr bgcolor="#9acd32">
                <th>Title</th>
                <th>Artist</th>
            </tr>
            <tr>
                <td>Greatest Hits</td>
                <td>Dolly Parton</td>
            </tr>
            <tr>
                <td>Eros</td>
                <td>Eros Ramazzotti</td>
            </tr>
            <tr>
                <td>Romanza</td>
                <td>Andrea Bocelli</td>
            </tr>
        </table>
    </body>
</html>
1
  • Thanks for your reply.Problem got fixed already.I used <xsl-stylesheet> instead of <xml-stylesheet>. Aug 28, 2012 at 13:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.