1
votes

I'm getting this error when trying to convert a XML document with XSL. I've only just started coding with XML, so I am sure I am missing something simple, but cant seem to find much information about it online.

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

<CATALOG>

<BOOK> 
    <NAME> 
        <TITLE>Caring for Sheep</TITLE> 
    </NAME>
    <PAGES> 60 </PAGES>
    <PRICE>$34.99</PRICE>
</BOOK>

</CATALOG> 

And my XSL File:

  Booklist.XSL

<?xml version="1.0"?>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="CATALOG/BOOK/NAME">
  <div style="background-color:teal;color:white;padding:4px">
    <a href="details.html"><xsl:value-of select="name">;</a>
  </div>
</xsl:for-each>

<xsl:for-each select="CATALOG/BOOK">
  <div style="background-color:teal;color:white;padding:4px">

  </div>
</xsl:for-each>

</body>

Can anyone see an error here?

2
If your XSL file is named Booklist.XSL but your XML document refers to it as booklist.xsl, and you use a type-sensitive filesystem, that could explain the problem. Otherwise, please tell us, what program are you running that gives the error you mentioned? Specifically, are you invoking an XSLT processor and giving it the XSL file?LarsH
The main problem with your new XSL file is that it is not an XSLT stylesheet.michael.hor257k
You just need to xslt namespace "<xsl:stylesheet xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:xs="w3.org/2001/XMLSchema" version="1.0">"Kulbhushan Singh
You don't find errors by staring at the code as you are asking us to do. You start with the error message and line number, which you haven't given us. If you got an error message, why haven't you told us what it is? If you didn't, then you're developing/testing this XSLT using the wrong tools.Michael Kay

2 Answers

0
votes

You're sort of mixing the Literal Result Element as Stylesheet simplified syntax with the regular syntax by putting a literal result element <body> outside any template, yet not at the top level. If you want to use this simplified syntax, get rid of the <xsl:stylesheet> and <xsl:output> elements, so that <body> is at the top level.

Also, XML and XPath are case-sensitive with regard to element names, so you'll need to fix the inconsistency between e.g. "catalog" and <CATALOG>.

0
votes

Well, if the for some reasons which are hard to figure out the simplified syntax doesn't work for you I would suggest using the "normal coding", which would be something like this in your case:

<?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">

    <xsl:template match="/">
      <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
        <xsl:for-each select="/CATALOG/BOOK/NAME">
            <div style="background-color:teal;color:white;padding:4px">
                <a href="details.html"><xsl:value-of select="name"/></a>
            </div>
        </xsl:for-each>

        <xsl:for-each select="/CATALOG/BOOK">
            <div style="background-color:teal;color:white;padding:4px">

            </div>
        </xsl:for-each>

     </body>  
    </xsl:template>
</xsl:stylesheet>