0

I am trying to copy all the child elements of soap:Header element into a variable and also trying to remove PricingRequest empty namespace (xmlns="http://www.ama.net") and output only PricingRequest part of xml. I am able to remove PricingRequest empty namespace and get proper output but not able to store soap:Header child elements into a variable, is it possible to copy child elements of soap:Header into a variable ?

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 :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://www.ama.net" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
<!--    <xsl:template match="/*[local-name()='Envelope']">
        <xsl:variable name="SOAPHeader" select="/soap:Header/node()" />
        <xsl:text> MessageHeader INFO:<xsl:copy-of select="$SOAPHeader"/>  </xsl:text>
    </xsl:template>  -->

    <xsl:template match="*">
        <xsl:element name="{name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>      
    </xsl:template>
    <xsl:template match="*[not(ancestor-or-self::x:PricingRequest)]">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

getting expecting OUTPUT but not able to copy soap:Header into a variable:

<PricingRequest>
  <originatorSection>
    <deliverySystem>
      <companyId>1A</companyId>
      <cityCode>MUC</cityCode>
    </deliverySystem>
  </originatorSection>
</PricingRequest>
2
  • You say "copy child elements of soap:Header" but you're showing the descendants of soap:Body. Commented Nov 5, 2014 at 1:22
  • hi Michael, thanks for looking into my question. The above xslt is able to get the output XML what i am looking for and for logging purpose, i need to print all elements and attributes inside soap:Header. if you look at my xslt, you can see what i am trying to do in commented code. I am not able to match soap:Header and copy its content into a variable Commented Nov 5, 2014 at 4:28

2 Answers 2

1

Try;

<xsl:template match="/">
    <xsl:variable name="SOAPHeader" select="/soap:Envelope/soap:Header/node()" />
    <xsl:text>MessageHeader INFO:</xsl:text>
    <xsl:copy-of select="$SOAPHeader"/>  
</xsl:template>

Note:

  1. <xsl:text> cannot contain child elements;
  2. your version has conflicting templates for soap:Envelope.
Sign up to request clarification or add additional context in comments.

3 Comments

I Michael, thanks for information on xsl:text , i used your code and re-framed my initial question. i am trying to transform to a new XML without namespaces, is it possible to do that ?
@sarma I suggest you roll back your question to the original and post a new one (after closing this one).
Thanks for your suggestion, i rolled back to the original question.
0

the following code worked the way i wanted. I am able to copy soap:Header child elements in a variable and also removed PricingRequest namespace in the output

    <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>         
        <xsl:text>HeaderInfo:</xsl:text>
        <xsl:copy-of select="$SOAPHeader" />
        <xsl:apply-templates select="/soap:Envelope/soap:Body/node()" />    
    </xsl:template>

</xsl:stylesheet>

OUTPUT I am getting:

<?xml version="1.0"?>
HeaderInfo:
  <MessageHeader xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 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>

<PricingRequest>
    <originatorSection>
        <deliverySystem>
            <companyId>1A</companyId>
            <cityCode>MUC</cityCode>
        </deliverySystem>       
    </originatorSection>    
</PricingRequest>

Comments

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.