0
votes

Can you please help me with the XSLT code to transform the below XML -

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:PrintCertificateByContractNumber xmlns:ns0="http://tempuri.org/">
   <ns0:ContractNumber>300001111</ns0:ContractNumber>
   <ns0:Credentials>
      <ns1:Password xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">uuuuuuuuuuuuuu</ns1:Password>
      <ns1:ResponseStatusList xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">
         <ns2:ResponseStatus xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
            <ns2:ErrorMessage/>
            <ns2:ResponseType>Error</ns2:ResponseType>
         </ns2:ResponseStatus>
      </ns1:ResponseStatusList>
      <ns1:UserName xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">xxxxxxxxxxxxxxxx</ns1:UserName>
      <ns1:UserReferenceNumber xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common"/>
   </ns0:Credentials>
</ns0:PrintCertificateByContractNumber>

Output XML that is needed:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
  <soap:Header/>
  <soap:Body>
    <tem:PrintCertificateByContractNumber>
      <tem:ContractNumber>?</tem:ContractNumber>
      <tem:Credentials>
        <ep4:Password>uuuuuuuu</ep4:Password>
        <ep4:ResponseStatusList>
          <ep41:ResponseStatus>
            <ep41:ErrorMessage></ep41:ErrorMessage>
            <ep41:ResponseType>Error</ep41:ResponseType>
          </ep41:ResponseStatus>
        </ep4:ResponseStatusList>
        <ep4:UserName>xxxxxxxx</ep4:UserName>
        <ep4:UserReferenceNumber></ep4:UserReferenceNumber>
      </tem:Credentials>
    </tem:PrintCertificateByContractNumber>
  </soap:Body>
</soap:Envelope>

XSLT code which I am using:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="ns0 ns1 ns2" xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ns0="http://tempuri.org/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output version="1.0" encoding="UTF-8" method="xml"/>
   <xsl:template match="/">
      <soapenv:Envelope xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:tem="http://tempuri.org/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
         <soapenv:Header/>
         <soapenv:Body>
            <tem:PrintCertificateByContractNumber>
               <tem:ContractNumber>
                  <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:ContractNumber"/>
               </tem:ContractNumber>
               <tem:Credentials>
                  <ep4:Password>
                     <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:Password"/>
                  </ep4:Password>
                  <ep4:ResponseStatusList>
                     <ep41:ResponseStatus>
                        <ep41:ErrorMessage>
                           <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:ResponseStatusList/ns2:ResponseStatus/ns2:ErrorMessage"/>
                        </ep41:ErrorMessage>
                        <ep41:ResponseType>
                           <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:ResponseStatusList/ns2:ResponseStatus/ns2:ResponseType"/>
                        </ep41:ResponseType>
                     </ep41:ResponseStatus>
                  </ep4:ResponseStatusList>
                  <ep4:UserName>
                     <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:UserName"/>
                  </ep4:UserName>
                  <ep4:UserReferenceNumber>
                     <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:UserReferenceNumber"/>
                  </ep4:UserReferenceNumber>
               </tem:Credentials>
            </tem:PrintCertificateByContractNumber>
         </soapenv:Body>
      </soapenv:Envelope>
   </xsl:template>
</xsl:stylesheet>

Output I am getting:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
    <tem:PrintCertificateByContractNumber xmlns:tem="http://tempuri.org/">
      <tem:ContractNumber/>
      <tem:Credentials>
        <ep4:Password xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" />
        <ep4:ResponseStatusList xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">
          <ep41:ResponseStatus xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
            <ep41:ErrorMessage/>
            <ep41:ResponseType/>
          </ep41:ResponseStatus>
        </ep4:ResponseStatusList>
        <ep4:UserName xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" />
        <ep4:UserReferenceNumber xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" />
      </tem:Credentials>
    </tem:PrintCertificateByContractNumber>
  </soapenv:Body>
</soapenv:Envelope>

Not able to remove namespaces from "Password" etc. from the mapping that I am using. Can you please let me know how to remove the namespaces from the final output XML.

Also, it would be highly appreciated anybody can tell me a generic code to achieve this, so that i can remove the XLST without going into copying the data for every node.

1

1 Answers

0
votes

AFAICT, the output you get is semantically identical to the output you want. The placement of namespace declarations is entirely insignificant and you have very little control over it, at least in XSLT 1.0.

It should not matter to the receiving application - and if it does, the problem should be fixed there. You can try forcing the namespace declarations to the root element by making your header:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://tempuri.org/" 
xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" 
xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/"
xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" 
xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" 
exclude-result-prefixes="ns0 ns1 ns2" >

and then changing:

<soapenv:Envelope xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:tem="http://tempuri.org/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

to:

<soapenv:Envelope>
    <xsl:copy-of select="document('')/xsl:stylesheet/namespace::tem | document('')/xsl:stylesheet/namespace::ep4 | document('')/xsl:stylesheet/namespace::ep41"/>

This may or may not work, depending on your XSLT processor.