dateboyTom
dateboyTom

Reputation: 109

After adding namespace cannot get required output xml

If I add namespace xmlns="http://www.yahoo.com/xmlns/ApplicationTest" as

<?xml version="1.0" xmlns="http://www.yahoo.com/xmlns/ApplicationTest" encoding="UTF-8"?>

in both usa11.xml and usa22.xml in my post:

Updating info in one XML file with optional info from another, using XSLT

Looks like that xslt cannot output expected results; It works perfect without this xmlns="http://www.yahoo.com/xmlns/ApplicationTest"

Pls help how to use XSL to fix it??

Thanks'

Upvotes: 0

Views: 71

Answers (2)

hr_117
hr_117

Reputation: 9627

To make the stylesheet from the previous answer work, add the namespace declaration to the stylesheet (with a prefix) and use the new namespace prefix for all node names.

Therefore this should do:

<?xml version="1.0" encoding="utf-8"?>

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

    <xsl:variable name="u2" select="document('usaa22.xml')"/>
    <xsl:template match="y:city">
        <xsl:choose>
            <xsl:when test="$u2//y:city[y:street=current()/y:street]">
                <xsl:copy>
                    <xsl:apply-templates select="$u2//y:city[y:street=current()/y:street]/* " />
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@* | node() " />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="@* | node() " />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Attention: Your xml change seems not be correct:

<?xml version="1.0" xmlns="http://www.yahoo.com/xmlns/ApplicationTest" encoding="UTF-8"?>

The <?xml prologue does not allow a namespace.
The namespace should be added to the first element in your xml file:

<country xmlns="http://www.yahoo.com/xmlns/ApplicationTest">

Upvotes: 1

Borodin
Borodin

Reputation: 126742

If you need to refer to any elements in the XML data whose names belong to a namespace, you have to assign a prefix for that namespace in the stylesheet. Within the stylesheet, XSLT considers every element with an unprefixed name in an XPath expression or match pattern to have no namespace, even if there is a default xmlns="http://..." definition.

So you have to write something like

xmlns:app="http://www.yahoo.com/xmlns/ApplicationTest"

and then prefix all references to nodes in your target XML data with the new namespace, like app:root.

I can't give a better example without seeing your live data. I hope that's clear.

Upvotes: 2

Related Questions