Rebbeca
Rebbeca

Reputation: 87

Unable to convert XML (containing namespaces) to XHMTL using XSLT

I am new to XML. My XML file contains two namespaces. I can easily transform XML to XHTML using XSLT if no namespace is defined. When i try to use namespaces in XSLT using path expressions, it does not work. Here's my code.

data.xml

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="style.xsl" ?>
<a:personData xmlns:a="http://auc.com">
 <a:user id="1">
    <a:fname>Assad</a:fname>
    <a:lname>Ch</a:lname>
    <a:email>[email protected]</a:email>
 </a:user>

 <a:user id="2">
    <a:fname>John</a:fname>
    <a:lname>Smith</a:lname>
    <a:email>[email protected]</a:email>
 </a:user>  
</a:personData>

style.xsl

<?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>
        <head>
            <title>Some Title</title>
        </head>
        <body>
            <p>
               <xsl:value-of select="a:personData/a:user/a:fname" />
            </p>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

i can't figure out what is wrong with my code.

Upvotes: 0

Views: 40

Answers (1)

Stamatis Liatsos
Stamatis Liatsos

Reputation: 74

As far as I can see, you are missing two things. In your xml the closing tag for the personData element is missing the namespace prefix, it should be

</a:personData>

Also in your xslt you should include the namespace as well,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

should become

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://auc.com">

Upvotes: 1

Related Questions