Reputation: 63
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>
Upvotes: 0
Views: 1243
Reputation: 63
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>
Upvotes: 0
Reputation: 116993
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:
<xsl:text>
cannot contain child elements;soap:Envelope
.Upvotes: 1