zar
zar

Reputation: 12237

XML file with namespace doesn't load with XSLT

I have the my XML file as below:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="menu.xsl" type="text/xsl" xmlns:k="Kitchen" ?>
<k:breakfast_menu>

<k:food>
<k:name>Belgian Waffles</k:name>
<k:price>$5.95</k:price>
<k:description>Two of our famous Belgian Waffles with plenty of real maple syrup</k:description>
<k:calories>650</k:calories>
</k:food>

</k:breakfast_menu>

The XSLT file is:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:k="Kitchen">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="k:breakfast_menu/k:food">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="k:name"/> - </span>
    <xsl:value-of select="k:price"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="k:description"/>
    <span style="font-style:italic"> (<xsl:value-of select="k:calories"/> calories per serving)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</html>

But this doesn't load it.

I am working off an example and the original example works but it didn't use namespaces. I have added the xmlns:k="Kitchen" namespace and corresponding changes to both files but now the browser doesn't display the data.

What I really want to see is how the XSLT works when the the source XML file uses namespaces and hence my changes. Anyone can see what am I doing wrong?

Upvotes: 0

Views: 43

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

You've added xmlns:k="Kitchen" within a processing instruction, where it has no effect. It needs to go on an element.

Nice to see someone using "simplified stylesheets" - an underused feature, in my view.

Upvotes: 1

Related Questions