-1

I am trying to follow the basic example on the W3Schools for xslt.

xml.xml:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <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>

xslt.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<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 border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Title</th>
        <th style="text-align:left">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>

Whenever i try to do this locally outside of the W3Schools it doesn't work, getting the following error.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

Looking around stack overflow I can't seem to find an answer to my specific query as this is something that should be quite simple.

My question is: how does the xslt file know that i want it to use xml.xml as the xml to match on?

logically you would have thought you would have to include it in the xslt file. When i've looked around i've seen people do the following:

xml.xml:

<?xml version="1.0"?>
<?xml-stylesheet href="xsl.xsl" type="text/xsl" ?>

xsl.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">

But that doesn't work for me locally, any suggestions?

gardni
  • 1,204
  • 2
  • 22
  • 47
  • Are you trying this in Chrome? http://stackoverflow.com/questions/3828898/can-chrome-be-made-to-perform-an-xsl-transform-on-a-local-file – michael.hor257k Jul 21 '15 at 17:04
  • What does "*doesn't work for me lcoally*" mean, precisely? What do you do to test it? What results do you see? – Robᵩ Jul 21 '15 at 17:15
  • Also, in your final example, if the filenames you provide are actual, then the line should be: ``. – Robᵩ Jul 21 '15 at 17:15

1 Answers1

0

The XSLT file doesn't know that you want to use xml.xml. Rather, the XML file knows that you want to use xslt.xslt.

Add this line as the 2nd line to your xml.xml file:

<?xml-stylesheet href="xslt.xslt" type="text/xsl" ?>

Also, note that Chrome doesn't like using XSL with file://-style URLs. Either use a non-Chrome browser or use http:// URLs.

Robᵩ
  • 143,876
  • 16
  • 205
  • 276