Dino
Dino

Reputation: 561

XSLT namespace incorrect

Hi I currently have the following namespace in my XML file

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="products.xsl"?>
<products xmlns="http://localhost" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://localhost products.xsd">
    <product>
         <productname>Product 1</productname>
         <productname>Product 2</productname>
         <productname>Product 3</productname>
         <productdetails>Details go here</productdetails>
         .... other elements that I don't use....
    </product>
</products>

When I try to run my XSL file I don't get any product names, I can get the product names i I remove the namespaces in product root element. So my namespaces mys be wrong. Can someone tell me where I've gone wrong? My xsl files is like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="5.0" omit-xml-declaration="yes" indent="yes"
    doctype-system="about:legacy-compat"/>
<xsl:template match="/">
    <!--  HTML page starts here  -->
    <html>
        <head>
            <title>Products Page</title>
        </head>
        <body>
            <h2>Products</h2>
            <ul>
                <xsl:for-each select="products/product/productname">
                    <li>
                        <xsl:value-of select="."/>
                    </li>
                </xsl:for-each>
            </ul>
        </body>
    </html>
    <!--  HTML page ends here  -->
</xsl:template>
</xsl:stylesheet>

The results I am looking for is

...(the other XHTML tags)...
<ul>
    <li>Product 1</li>
    <li>Product 2</li>
    <li>Product 3</li>
</ul>
...(rest of XHTML tags)..

But I'm not getting that at all, all I get is:

<!DOCTYPE html SYSTEM "about:legacy-compat">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Products Page</title>
   </head>
 <body>
   <h2>Products</h2>
     <ul></ul>
 </body>
</html>

As you can see there is no products showing?

Upvotes: 0

Views: 221

Answers (2)

keshlam
keshlam

Reputation: 8058

Remember, XPath (and therefore XSLT) is namespace-aware, but (in the 1.0 version of these standards at least) does not have the concept of default namespace (xmlns=) assignment. If you want to select namespaced nodes, your XPath must use explicit prefixes bound to the correct namespaces.

Upvotes: 1

Related Questions