photo_tom
photo_tom

Reputation: 7342

Unable to Select node with namespace attached

I have the following XML Fragment

<?xml version="1.0" encoding="UTF-8"?>
<Sheet version="1.0" xmlns:j="http://www.it.ojp.gov/jxdm/3.0.2">
    <Subject xmlns="http://www.it.ojp.gov/jxdm/3.0.2">
        <PersonName>
            <PersonGivenName>EDWIN</PersonGivenName>
            <PersonMiddleName>J</PersonMiddleName>
            <PersonSurName>TURNER</PersonSurName>
        </PersonName>
    </Subject>
</Sheet>

I'm using trying to select the Subject node with the following XSLT

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
    <body>
        <xsl:value-of select="Sheet/Subject"/>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>

I was testing this segment on http://www.shell-tools.net/index.php?op=xslt.

What happens is that if I run the transform with the XML as written, the select attribute doesn't match. However, If I remove the namespace from the Subject node, it will correctly select the data.

I'm looking for the syntax as to how to make the selection work with namespace attached to the Subject node as this is how the data is received from a web service.

Upvotes: 1

Views: 53

Answers (1)

hek2mgl
hek2mgl

Reputation: 157927

You need to define an alias for the namespace of the sheet document. The xsl should look like this:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:sheet="http://www.it.ojp.gov/jxdm/3.0.2" <--- Define an alias
    exclude-result-prefixes="sheet"                <--- Prevent xslt from using this 
                                                        namespace in the output document
>
<xsl:template match="/">
<html>
    <body>
        <xsl:value-of select="Sheet/sheet:Subject"/>   <--- Use alias
    </body>
</html>
</xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions