user265330
user265330

Reputation: 2553

XSLT with namespace causing an issue

I want to grab "LIVE" out of the XML below. I've read similar posts and have been using the local-name() function as a result, but no matter what XSLT I use I can't get it.

<?xml version="1.0"?>
<cns:customer xmlns:cns="https://services.cns.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://services.cns.com docs/xsd/customer.xsd">
    <cns:userId>1001</cns:userId>
    <cns:status>LIVE</cns:status>
    <cns:type wholesale="true" suspended="false">W1</cns:type>
    <cns:properties>
        <cns:property>
            <cns:name>Name</cns:name>
            <cns:value>Bob</cns:value>
        </cns:property>
    </cns:properties>
</cns:customer>

Here is the XSLT I am using.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>
    <xsl:template match="//*[local-name()='status']/text()">
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

I am testing using Oxygen application. I think the processor is Saxon 6.5.5.

The output I get is:

1001
LIVE
W1

    Name
    Bob

Thanks, Paul

Upvotes: 0

Views: 275

Answers (2)

rapha&#235;λ
rapha&#235;λ

Reputation: 6523

Answer based on your your recent edit to the question.

just use:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>
    <xsl:template match="//*[local-name()='status']/text()">
        value: <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

The 1001, W1 ....etc are not matched by any template and are therefore handled by the default template ,which just echoes it into the output.

Now you also make the namespace version work:

<xsl:template match="/c:customer/c:status" xmlns:c="https://services.cns.com">
    value: <xsl:apply-templates select="text()"/>
</xsl:template>

Upvotes: 2

rapha&#235;λ
rapha&#235;λ

Reputation: 6523

You should register your namespaces with your XSLT processor. But if you somehow cannot, you should be able to execute the following XPath expression:

//*[local-name()="status"]/text()

Upvotes: 1

Related Questions