sarma
sarma

Reputation: 63

Not able to get attribute value, am I missing something?

  1. I am able to get values of MessageHeader child elements (/soap:Envelope/soap:Header/ns1:MessageHeader/ns1:From/text()) but not its attribute values (/soap:Envelope/soap:Header/ns1:MessageHeader/@ns1:ResponseRequested), I am not able to figure out where i am doing wrong. can someone look into it please ?
  2. I stored Messageheader node in a variable and trying to access its content, but not able to do xpath onto its concent, can someone please suggest on how can i do it ?

INPUT XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap ="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns="http://www.ama.net/1axml-msg/schema/msg-header-1_0.xsd">
  <MessageHeader ResponseRequested="true" version="1.0" Terminate="true" Reverse="true" id="09B5581A" soap:mustUnderstand="1">
    <From>1ASI</From>
    <To>1ASRINSAIG</To>
    <TimeStamp>
      <GenerationTime>2014-10-22T12:41:38Z</GenerationTime>
    </TimeStamp>    
  </MessageHeader>
</soap:Header>
<soap:Body>
<PricingRequest xmlns="http://www.ama.net">
    <originatorSection>
        <deliverySystem>
            <companyId>1A</companyId>
            <cityCode>MUC</cityCode>
        </deliverySystem>       
    </originatorSection>    
</PricingRequest>
</soap:Body>
</soap:Envelope>

XSLT trying:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.ama.net/1axml-msg/schema/msg-header-1_0.xsd"
    xmlns:ns2="http://www.ama.net" exclude-result-prefixes="ns1 ns2 soap">

    <xsl:template match="ns1:* | ns2:*| soap:*">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="/">
        <xsl:variable name="SOAPHeader">
            <xsl:apply-templates select="/soap:Envelope/soap:Header/node()" />
        </xsl:variable>         
From (Working):<xsl:copy-of select="/soap:Envelope/soap:Header/ns1:MessageHeader/ns1:From/text()"/>
From from $SOAPHeader (Not Working)::<xsl:copy-of select="$SOAPHeader/ns1:MessageHeader/ns1:From/text()"/>

ResponseRequested (NOT Working):    <xsl:value-of select="/soap:Envelope/soap:Header/ns1:MessageHeader/@ns1:ResponseRequested"/>
ResponseRequested from $SOAPHeader (Not Working):   <xsl:value-of select="$SOAPHeader/ns1:MessageHeader/@ns1:ResponseRequested"/>
    </xsl:template>

</xsl:stylesheet>

OUTPut getting:

<?xml version="1.0"?>

From (Working):1ASI
From(Not Working)::

Upvotes: 0

Views: 1360

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52848

You're stripping the namespace from the elements when you create SOAPHeader variable, so strip the prefix from the XPath...

<xsl:copy-of select="$SOAPHeader/MessageHeader/From/text()"/>

and

<xsl:value-of select="$SOAPHeader/MessageHeader/@ResponseRequested"/>

Also, if you're only getting the output you show and you're not getting any "ResponseRequest" text, it could be that you're running the XSLT as version 1.0 (there's no version in you xsl:stylesheet element). In 1.0, you can't use a result tree fragment in a path expression without using an extension function (like exsl:node-set()).

Upvotes: 2

Related Questions