XSLT to add namespace into XML doc

Hello All,

Could you please let me know why the below XSLT code is not functioning for the following XML. And, the same XSLT code works fine in XSLT tool/ validator that is available online. Please find the below code.

XML CODE:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <Mrn>8020001</Mrn>
   <oRequest>
      <AllowReuse></AllowReuse>
      <BillStreetAddr>18000</BillStreetAddr>
      <BillZip>91325</BillZip>
      <CardholderNm>TEST ME</CardholderNm>
      <CcType>
         <Id>0</Id>
         <Mnemonic>AMEX</Mnemonic>
         <Name>0</Name>
         <Number>0</Number>
      </CcType>
      <Com></Com>
      <ConfirmationFld>xxxxxxxxxxxx2009</ConfirmationFld>
      <CtySt>NORTHRIDGE,CA</CtySt>
      <ExpirationDt>2021-03-01</ExpirationDt>
      <PaymentAuthCcId>57c53676f7b148AAAA79bd74723d67c0</PaymentAuthCcId>
      <PaymentAuthLoc>
         <Id>0</Id>
         <Mnemonic>MDTM</Mnemonic>
         <Name>0</Name>
         <Number>0</Number>
      </PaymentAuthLoc>
      <RecordType>CC</RecordType>
   </oRequest>
</root>

XSLT :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://gehcit.ge.com/dme" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:dme="http://gehcit.ge.com/dme" 
xmlns:med="http://gehcit.ge.com/cb/xxx" 
xmlns:dict="http://gehcit.ge.com/cb/www" 
xmlns:dict1="http://gehcit.ge.com/cb/ddd" version="1.0">
<xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
<!-- Stylesheet to inject namespaces into a document in specific places -->
<xsl:template match="/">
<soapenv:Envelope>
<soapenv:Header/>
<soapenv:Body>
<xsl:choose>
<!-- Handle 'Root' wrapper added by JSON to XML policy -->
<xsl:when test="normalize-space(/Root)">
<dme:AddCreditCard>
<xsl:apply-templates select="node()|@*"/>
</dme:AddCreditCard>
</xsl:when>
<!-- Handle 'Array' wrapper added by JSON to XML policy -->
<xsl:when test="normalize-space(/Array)">
<dme:AddCreditCard>
<xsl:apply-templates select="node()|@*"/>
</dme:AddCreditCard>
</xsl:when>
<!-- If the root element is not what was in the schema, add it -->
<xsl:when test="not(normalize-space(/AddCreditCard))">
<dme:AddCreditCard>
<xsl:apply-templates select="node()|@*"/>
</dme:AddCreditCard>
</xsl:when>
<!-- everything checks out,  just copy the xml-->
<xsl:otherwise>
<xsl:apply-templates select="node()|@*"/>
</xsl:otherwise>
</xsl:choose>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
<xsl:template match="Mrn | oRequest" name="copy-root">
<xsl:element name="dme:{local-name()}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(local-name()='Mrn') and not(local-name()='oRequest') and not(local-name()='root')]" name="copy-all">
<xsl:element name="med:{local-name()}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="//CcType/*" name="copy-dict">
<xsl:element name="dict:{local-name()}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
       <xsl:template match="//PaymentAuthLoc/*" name="copy-dict1">
<xsl:element name="dict1:{local-name()}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<!-- template to copy the rest of the nodes -->
<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>

Expected result:

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:dme="http://gehcit.ge.com/dme" >
   <soapenv:Header/>
   <soapenv:Body>
      <dme:AddCreditCard>
         <dme:Mrn>?</dme:Mrn>
         <dme:oRequest>
            <med:CcType>
               <!--Optional:-->
               <dict:Id>?</dict:Id>
               <!--Optional:-->
               <dict:Name>?</dict:Name>
               <!--Optional:-->
               <dict:Number>?</dict:Number>
               <!--Optional:-->
               <dict:Mnemonic>?</dict:Mnemonic>
            </med:CcType>
            <med:ConfirmationFld>?</med:ConfirmationFld>
            <med:ExpirationDt>?</med:ExpirationDt>
            <med:CardholderNm>?</med:CardholderNm>
            <med:BillStreetAddr>?</med:BillStreetAddr>
            <med:CtySt>?</med:CtySt>
            <med:BillZip>?</med:BillZip>
            <!--Optional:-->
            <med:Com>?</med:Com>
            <!--Optional:-->
            <med:AllowReuse>?</med:AllowReuse>
            <!--Optional:-->
            <med:PaymentAuthLoc>
               <!--Optional:-->
               <dict1:Id>?</dict1:Id>
               <!--Optional:-->
               <dict1:Name>?</dict1:Name>
               <!--Optional:-->
               <dict1:Number>?</dict1:Number>
               <!--Optional:-->
               <dict1:Mnemonic>?</dict1:Mnemonic>
               <!--Optional:-->
               <dict1:ThirdPartyProcessorId>?</dict1:ThirdPartyProcessorId>
            </med:PaymentAuthLoc>
            <!--Optional:-->
            <med:RecordType>?</med:RecordType>
            <med:PaymentAuthCcId>?</med:PaymentAuthCcId>
         </dme:oRequest>
      </dme:AddCreditCard>
   </soapenv:Body>
</soapenv:Envelope>

Actual Result:

<soapenv:Envelope
    xmlns:ns="http://gehcit.ge.com/dme"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
  <soapenv:Header/>
  <soapenv:Body>
    <dme:AddCreditCard>
      <ns:mrn>8020001</ns:mrn>
      <ns:orequest>
        <ns:allowreuse/>
        <ns:billstreetaddr>18000</ns:billstreetaddr>
        <ns:billzip>91325</ns:billzip>
        <ns:cardholdernm>TEST ME</ns:cardholdernm>
        <ns:cctype>
          <ns:id/>
          <ns:mnemonic>AMEX</ns:mnemonic>
          <ns:name/>
          <ns:number/>
        </ns:cctype>
        <ns:com/>
        <ns:confirmationfld>xxxxxxxxxxxx2009</ns:confirmationfld>
        <ns:ctyst>NORTHRIDGE,CA</ns:ctyst>
        <ns:expirationdt>2021-03-01</ns:expirationdt>
        <ns:paymentauthccid>57c53676f7b148AAAA79bd74723d67c0</ns:paymentauthccid>
        <ns:paymentauthloc>
          <ns:id/>
          <ns:mnemonic>MDTM</ns:mnemonic>
          <ns:name/>
          <ns:number/>
        </ns:paymentauthloc>
        <ns:recordtype>CC</ns:recordtype>
      </ns:orequest>
    </dme:AddCreditCard>
  </soapenv:Body>
</soapenv:Envelope>

0 3 3,489
3 REPLIES 3

@Dino

Do you got some one who could assist me on this ?

hi Karthik

First, looking at the XML that is "desired" and "actual result" - neither are well formed. They use namespace prefixes that have not been declared. In the desired example, the prefixes med, dict, and dict1 are all used, but not declared. In the actual, the dme prefix is used, but not declared.

When I tried your XSL, I got something different from the actual result, pretty close to your desired result. My observed output was well-formed, it had all the prefixes declared. Why are my actual results different from yours? Maybe there is a cut-and-paste issue.

From my API Proxy with the XSL policy, I get this result:

<soapenv:Envelope xmlns:ns="http://gehcit.ge.com/dme"
                  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:dme="http://gehcit.ge.com/dme"
                  xmlns:med="http://gehcit.ge.com/cb/xxx"
                  xmlns:dict="http://gehcit.ge.com/cb/www"
                  xmlns:dict1="http://gehcit.ge.com/cb/ddd">
   <soapenv:Header/>
   <soapenv:Body>
      <dme:AddCreditCard>
         <dme:Mrn>8020001</dme:Mrn>
         <dme:oRequest>
            <med:AllowReuse/>
            <med:BillStreetAddr>18000</med:BillStreetAddr>
            <med:BillZip>91325</med:BillZip>
            <med:CardholderNm>TEST ME</med:CardholderNm>
            <med:CcType>
               <dict:Id>0</dict:Id>
               <dict:Mnemonic>AMEX</dict:Mnemonic>
               <dict:Name>0</dict:Name>
               <dict:Number>0</dict:Number>
            </med:CcType>
            <med:Com/>
            <med:ConfirmationFld>xxxxxxxxxxxx2009</med:ConfirmationFld>
            <med:CtySt>NORTHRIDGE,CA</med:CtySt>
            <med:ExpirationDt>2021-03-01</med:ExpirationDt>
            <med:PaymentAuthCcId>57c53676f7b148AAAA79bd74723d67c0</med:PaymentAuthCcId>
            <med:PaymentAuthLoc>
               <dict1:Id>0</dict1:Id>
               <dict1:Mnemonic>MDTM</dict1:Mnemonic>
               <dict1:Name>0</dict1:Name>
               <dict1:Number>0</dict1:Number>
            </med:PaymentAuthLoc>
            <med:RecordType>CC</med:RecordType>
         </dme:oRequest>
      </dme:AddCreditCard>
   </soapenv:Body>
</soapenv:Envelope>


...which looks like it will satisfy.

Attached please fined the working api proxy I used. karthik-xsl1-apiproxy.zip

Do you get these results? If not, we need to understand why not.

If you DO get these results, is this what you are expecting? If not, please explain exactly what is unacceptable about the actual result shown above.

Hello @Dino,

Due to confidentiality of data, i didnt paste actual XML file. Let me have a look at attachment of your work.

Thank you so much 🙂

Regards,

Karthik