Kumar_y
Kumar_y

Reputation: 239

Xpath1.0 local-name() versus namespace in XSLT

For some reason, when I am using proper XPath without local-name(), I am not getting any value.

Non-working XPaths I have tried:

  1. //mon:ReturnUpdateRequest/urn:Header/urn:DocumentIdentifier/text()
  2. soapenv:Envelope/soapenv:Body/mon:ReturnUpdateRequest/urn:Header/urn:DocumentIdentifier/text()

Both above expressions give the correct value in XPath tool and XMLspy.

Working XPath using local-name():

/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ReturnUpdateRequest']/*[local-name()='Header']/*[local-name()='DocumentIdentifier']

Input request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mon="Monsanto:ServiceReturnUpdate" xmlns:urn="urn:monsanto:uscomm:service:header">
    <soapenv:Body>
        <mon:ReturnUpdateRequest Version="?">
            <urn:Header>
                <urn:DocumentIdentifier>1013083</urn:DocumentIdentifier>
                <urn:DocumentDateTime>2015-06-11T17:46:11.092-03:00</urn:DocumentDateTime>
                <urn:From>
                    <urn:PartnerName>1013083</urn:PartnerName>
                    <urn:PartnerIdentifier type="SAP_ID">1013083</urn:PartnerIdentifier>
                    <!--Zero or more repetitions:-->
                </urn:From>
                <!--Optional:-->
                <urn:To>
                    <urn:PartnerName>1013083</urn:PartnerName>
                    <urn:PartnerIdentifier type="SAP_ID">0001013083</urn:PartnerIdentifier>
                    <!--Zero or more repetitions:-->
                </urn:To>
                <urn:DataSource>APP</urn:DataSource>
                <!--Optional:-->
                <urn:SoftwareVersion>?</urn:SoftwareVersion>
            </urn:Header>
        </mon:ReturnUpdateRequest>
    </soapenv:Body>
</soapenv:Envelope>

Code Snippet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mon="ServiceReturnDetail" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
            <soapenv:Header/>
            <soapenv:Body>
                <urn:YSdsaUsseedRetUpdate>
                    <IIdent>
                        <!--    <xsl:value-of select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ReturnUpdateRequest']/*[local-name()='Header']/*[local-name()='DocumentIdentifier']"/>-->
                        <xsl:value-of select="//mon:ReturnUpdateRequest/urn:Header/urn:DocumentIdentifier/text()"/>
                    </IIdent>
                    <ISrc>
                        <!--<xsl:value-of select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ReturnUpdateRequest']/*[local-name()='Header']/*[local-name()='DocumentDateTime']"/>-->
                        <xsl:value-of select="/soapenv:Envelope/soapenv:Body/mon:ReturnUpdateRequest/urn:Header/urn:DocumentDateTime"/>
                    </ISrc>
                </urn:YSdsaUsseedRetUpdate>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

Where I am doing it wrong?

Upvotes: 1

Views: 1399

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

where I am doing it wrong?

In your input XML, the ReturnUpdateRequest is in a namespace whose URI is "Monsanto:ServiceReturnUpdate". You are trying to address it as mon:ReturnUpdateRequest - but your stylesheet binds the mon prefix to a completely different URI:

xmlns:mon="ServiceReturnDetail"

You have a similar problem with the urn prefix.

Here's a working example:

<xsl:stylesheet version="1.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:mon="Monsanto:ServiceReturnUpdate" 
xmlns:hdr="urn:monsanto:uscomm:service:header">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
            <soapenv:Header/>
            <soapenv:Body>
                <urn:YSdsaUsseedRetUpdate>
                    <IIdent>
                        <xsl:value-of select="//mon:ReturnUpdateRequest/hdr:Header/hdr:DocumentIdentifier"/>
                    </IIdent>
                </urn:YSdsaUsseedRetUpdate>
            </soapenv:Body>
        </soapenv:Envelope>
</xsl:template>

</xsl:stylesheet>

Note: since you want to use the same urn prefix in the output but bind it to a different namespace, I have used a different prefix to address the input. What matters here is the URI, not the prefix itself.

Upvotes: 1

Related Questions