0

I have an XML File:

 <?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet hred="remakes.xsl" type="text/xsl" ?>
<remakes>
<remake>
<rtitle>Pygmalion</rtitle>
<ryear>1938</ryear>
<fraction>0.5</fraction>
<stitle>Pygmalion</stitle>
<syear>1937</syear>
</remake>...

and I have created a stylesheet:

 ?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/remakes">
<html>
<body>
<table border="1">
    <tr>
        <th>rtitle<</th>
        <th>fraction<</th>
        <th>stitle<</th>
        <th>syear<</th>
    </tr>
    <xsl:for-each select="remake">
        <xsl:value-of select="rtitle"/></td>
        <xsl:value-of select="fraction"/></td>
        <xsl:value-of select="stitle"/></td>
        <xsl:value-of select="syear"/></td>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

But browsers don't parse it so instead of a table there is chaos. Is there a mistake in the code?

v0id
  • 29
  • 5

1 Answers1

0

There are a number of problems with your stylesheet, although they could always be typos with your question, but as there are too many to mention in comments, they are as follows:

  1. The namespace prefix xsl has not been bound. You should type xmlns:xsl="..."
  2. There is a < symbol in each table header <th>rtitle<</th>. If you really wanted this, you should write it as <th>rtitle&lt;</th>, but more likely it should just be <th>rtitle</th>
  3. You have a closing <\td> for each row, but no opening <td> tag.
  4. You also really need to wrap those <td> in a <tr> tag for it to be rendered properly.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/remakes">
<html>
<body>
<table border="1">
    <tr>
        <th>rtitle</th>
        <th>fraction</th>
        <th>stitle</th>
        <th>syear</th>
    </tr>
    <xsl:for-each select="remake">
        <tr>
            <td><xsl:value-of select="rtitle"/></td>
            <td><xsl:value-of select="fraction"/></td>
            <td><xsl:value-of select="stitle"/></td>
            <td><xsl:value-of select="syear"/></td>
        </tr>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Tim C
  • 68,447
  • 14
  • 67
  • 90
  • 1
    Also `hred` needs to be `href` in the XML. – michael.hor257k Mar 20 '17 at 09:28
  • thank you, I didn't notice my stupid spelling mistakes :)it works in Firefox, but not in Chrome:( – v0id Mar 20 '17 at 09:55
  • If you running the XML / XSLT locally from your file system (rather than from a web server), then Chrome disallows this. See http://stackoverflow.com/questions/3828898/can-chrome-be-made-to-perform-an-xsl-transform-on-a-local-file – Tim C Mar 20 '17 at 10:09