-1

Beginner to XSL transformation, looking for help to convert the data using xsl.

Input data is as below:

<?xml version="1.0" encoding="UTF-8"?>
 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
   <abcRequest xmlns="http://google.com/2018/abcService">
     <messageHeader>
        <Id>000000</Id>
        <aId>572b0285-7e06-4834-90c0-dc45eeeafe70</aId>
        <version>1.0</version>
     </messageHeader>
   </abcRequest>
  </soap:Body>
</soap:Envelope>

Expected output data is as below:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:p="http://www.bnppf.com/20190101/TA/TA99eWLNotificationDataSrv">
  <soap:Header>
    <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://www.gmail.com/20190101/newService</wsa:Action>
  </soap:Header>
  <soap:Body>
    <p:abcRequest xmlns="http://google.com/2018/abcService">
     <p:messageHeader>
        <p:Id>000000</p:Id>
        <p:aId>572b0285-7e06-4834-90c0-dc45eeeafe70</p:aId>
        <p:version>1.0</p:version>
     </p:messageHeader>
    </p:abcRequest>
  </soap:Body>
</soap:Envelope>
  • What have you tried so far? – user871611 Mar 05 '19 at 19:54
  • "looking for help" is not a question. – michael.hor257k Mar 05 '19 at 19:54
  • Hi, welcome to Stack Overflow. Remember that this is not a code-writing service, and that you are expected to have a go yourself before coming here. Please edit the question to include the XSL you have written so far, to show the output you are getting in practice, and to explain where you are getting stuck. If you genuinely don't know where to start, please identify the documentation/articles/examples you have looked at so far, and why those did not help. Thanks. – MandyShaw Mar 05 '19 at 21:33

1 Answers1

0

You can achieve the transformation with the following stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:abc="http://google.com/2018/abcService" xmlns:p="http://www.bnppf.com/20190101/TA/TA99eWLNotificationDataSrv">
  <xsl:output method="xml"/>

  <xsl:template match="node()|@*">
      <xsl:copy>
          <xsl:apply-templates select="node()|@*" />
      </xsl:copy>
  </xsl:template>

  <xsl:template match="soap:Envelope">
    <soap:Envelope>
        <soap:Header>
          <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://www.gmail.com/20190101/newService</wsa:Action>
        </soap:Header>
        <xsl:apply-templates select="soap:Body" />
    </soap:Envelope>
  </xsl:template>

  <xsl:template match="abc:*">
      <xsl:element name="p:{local-name()}">
          <xsl:apply-templates select="node()|@*" />
      </xsl:element>
  </xsl:template> 

</xsl:stylesheet>

But the following default xmlns namespace on your p:abcRequest element will not be created because it is superfluous.

<p:abcRequest xmlns="http://google.com/2018/abcService">

Instead, the xmlns:p namespace will be set for this element and its children.
The output is:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:abc="http://google.com/2018/abcService" xmlns:p="http://www.bnppf.com/20190101/TA/TA99eWLNotificationDataSrv">
    <soap:Header>
        <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://www.gmail.com/20190101/newService</wsa:Action>
    </soap:Header>
    <soap:Body>
        <p:abcRequest>
            <p:messageHeader>
                <p:Id>000000</p:Id>
                <p:aId>572b0285-7e06-4834-90c0-dc45eeeafe70</p:aId>
                <p:version>1.0</p:version>
            </p:messageHeader>
        </p:abcRequest>
    </soap:Body>
</soap:Envelope>
zx485
  • 28,498
  • 28
  • 50
  • 59
  • thank u The namespaces (xmlns:abc, xmlns:p) are added to header (instead of Envelope). I get below o/p http://www.gmail.com/20190101/newService – user8491771 Mar 05 '19 at 21:16
  • @user8491771: I changed my answer. Now, the namespace definitions do appear on the `soap:Envelope` root element. I hope that this fixes your problem. – zx485 Mar 05 '19 at 21:25