Deena
Deena

Reputation: 335

Unable to get XSLT output with custom namespace

Here is the format of my xml:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://www.example.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <child>
                <element1>xyz</element1>
                <element2>def</element2>
        </child>
</Root>

Here is my xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns="http://www.example.com/xyz"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/">
        <xsl:for-each select="Root/child">
            <xsl:value-of select="element1"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

This does not have any output

When I take the url out of the XML file, I get the output without any problems. However, I would like to get this to work with the namespace working correctly.

I tried a few adjustments based on other questions that I found online: 1) changing the match="/" to "match=Root" - This just printed out the entire file with no tags

2) changing the xmlns="http://www.example.com/xyz" to xmlns:Root="http://www.example.com/xyz" - this still didn't print anything out.

I have read a few other similar questions, but the formatting was different enough that it didn't work with my situation.

Upvotes: 0

Views: 620

Answers (1)

cwschmidt
cwschmidt

Reputation: 1204

You have to qualify every element within your custom namespace also with the namespace prefix within your xslt, e.g.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:aa="http://www.example.com/xyz"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/">
        <xsl:for-each select="aa:Root/aa:child">
            <xsl:value-of select="aa:element1"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

The namespace prefix here is 'aa'.

You create the association between namespace and prefix here

xmlns:aa="http://www.example.com/xyz"

and you have to use the same prefix 'aa' to refer to the elements 'Root', 'child', 'element1' and 'element2'

like here

<xsl:for-each select="aa:Root/aa:child">
    <xsl:value-of select="aa:element1"/>
</xsl:for-each>

You can also choose a different prefix, 'aa' is just an example.

'Root' also is inside the namespace, because the namespace definition in the source xml document already includes the element where the namespace is introduced:

<Root xmlns="http://www.example.com/xyz"> ... </Root>

In your souce document xml you haven't assigned a namespace prefix. If you want to a assign prefix there also, it would look like

<?xml version="1.0" encoding="utf-8"?>
<bb:Root xmlns:bb="http://www.example.com/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <bb:child>
                <bb:element1>xyz</bb:element1>
                <bb:element2>def</bb:element2>
        </bb:child>
</bb:Root>

The prefix here is 'bb'. As you see the prefix need not match the prefix in the stylesheet.

Upvotes: 1

Related Questions