0

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'

2
  • 1
    Perhaps you should start decide which answers to your previous questions are valid, or tell why none. Commented May 6, 2013 at 18:34
  • An xmlns attribute in the XML declaration? Where did you get that idea from? Commented May 6, 2013 at 22:28

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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">

1 Comment

@LarsH: Thank you very much, for fixing and correcting my bad English.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.