1

The XML namespaces are creating a problem for me and I am not able to pick any value. I believe this is happening because some tags are having namespaces or some are not.

Can you please help me to create an XSLT to get the desired output?

Sample Input -

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns3:NotificationResponse xmlns:ns3="http://www.test.com/ns/wsdl/test-V2_4" xmlns="http://www.test.com/ns/xsd/test-V2_1" xmlns:ns2="http://www.test.com/ns/xsd/test-V2_2">
         <Header>
            <appCode />
            <isRemNot>false</isRemNot>
            <identification>Test</identification>
            <msgDT>2018-05-01T16:29:12.937+02:00</msgDT>
         </Header>
         <Contract />
         <Notification>
            <ns2:notificationTypeCode>TestTypeCode</ns2:notificationTypeCode>
         </Notification>
      </ns3:NotificationResponse>
   </soap:Body>
</soap:Envelope>

Sample Output -

<Contract>
            <isRemNot>false</isRemNot>
        <identification>Test</identification>
        <msgDT>2018-05-01T16:29:12.937+02:00</msgDT>
            <notificationTypeCode>TestTypeCode</notificationTypeCode>
<Contract>
Amit Sahoo
  • 13
  • 3
  • If you search for "XSLT default namespace" you will find hundreds of answers to this question. – Michael Kay May 03 '18 at 11:37
  • @MichaelKay I had no idea on xmlns (a newbie).. hence I was no able to understand this.. but the answer below and your link makes sense now. – Amit Sahoo May 03 '18 at 13:32
  • Yes, the reason so many people ask this question is that they generally have no idea what's causing the problem and their searches are therefore generally unsuccessful. The answer is: before you start coding and hitting problems, sit down for an evening with a good book and read about the concepts. – Michael Kay May 03 '18 at 14:32

1 Answers1

0

some tags are having namespaces or some are not. - Input XML elements not prefixed with namespace prefix are associated to the namespace xmlns="http://www.test.com/ns/xsd/test-V2_1". In order to fetch data, you will have to map all the namespaces in the XSLT and access the elements accordingly.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns3="http://www.test.com/ns/wsdl/test-V2_4"
    xmlns:ns1="http://www.test.com/ns/xsd/test-V2_1"
    xmlns:ns2="http://www.test.com/ns/xsd/test-V2_2" 
    exclude-result-prefixes="soap ns1 ns2 ns3">

If you do not need the namespaces in the output XML, you can use the exclude-result-prefixes attribute.

In this case namespace "http://www.test.com/ns/xsd/test-V2_1" has been identified using prefix ns1 in the XSL and the elements viz. <Header>, <isRemNot>, etc. are accessed as <ns1:Header>, <ns1:isRemNot> and so on.

Below is the template that you can use.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns3="http://www.test.com/ns/wsdl/test-V2_4"
    xmlns:ns1="http://www.test.com/ns/xsd/test-V2_1"
    xmlns:ns2="http://www.test.com/ns/xsd/test-V2_2" 
    exclude-result-prefixes="soap ns1 ns2 ns3">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="ns3:NotificationResponse">
        <Contract>
            <isRemNot><xsl:value-of select="ns1:Header/ns1:isRemNot" /></isRemNot>
            <identification><xsl:value-of select="ns1:Header/ns1:identification" /></identification>
            <msgDT><xsl:value-of select="ns1:Header/ns1:msgDT" /></msgDT>
            <notificationTypeCode><xsl:value-of select="ns1:Notification/ns2:notificationTypeCode" /></notificationTypeCode>
        </Contract>
    </xsl:template>
</xsl:stylesheet>

Output

<Contract>
    <isRemNot>false</isRemNot>
    <identification>Test</identification>
    <msgDT>2018-05-01T16:29:12.937+02:00</msgDT>
    <notificationTypeCode>TestTypeCode</notificationTypeCode>
</Contract>
Aniket V
  • 3,183
  • 2
  • 15
  • 27