简体   繁体   中英

add same namespace to xml using xsl

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B" 
    xmlns:max="http://mynamespace/C"><soapenv:Body>
  <t1:Creditcard>
     <top:AutoPayenroll>
        <top:CustomerId>
           <max:CustName>Taylor</max:CustName>
           <max:CustID>1234</max:CustID>
        </top:CustomerId>
     </top:AutoPayenroll>
  </t1:CreditCard></soapenv:Body></soapenv:Envelope>  

Need to change the CustID to encrypted one which I did. but dont know how to insert it

Used this XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B"
  xmlns:max="http://mynamespace/C" version="1.0">
  <xsl:output method="xml"/><xsl:template match="/"> 
<xsl:apply-templates/>   </xsl:template>  <xsl:template match="//*[local-name()='CustID']"> 
<xsl:variable name="cleartxt" select="./text()"/>  
<!--got this encrypted data from my internal code-->  
<xsl:variable name="encdata" select="'jksdguasidgeiruh'"/>  
<xsl:element name="//*[local-name()='Pswd']"> 
  <xsl:value-of select="$encdata"/> 
</xsl:element>   </xsl:template>   <xsl:template match="*"> 
<xsl:copy> <xsl:copy-of select="@*"/>  
  <xsl:apply-templates/> 
</xsl:copy>   </xsl:template> </xsl:stylesheet>    

Response should be as follows:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B" xmlns:max="http://mynamespace/C"><soapenv:Body>
  <t1:Creditcard>
     <top:AutoPayenroll>
        <top:CustomerId>
           <max:CustName>Taylor</max:CustName>
       <max:CustID>jksdguasidgeiruh</max:CustID>
        </top:CustomerId>
     </top:AutoPayenroll>
  </t1:CreditCard></soapenv:Body></soapenv:Envelope>      

Your example XSL and desired output are a little at odds, but whatever.

Have you tried something like:

<max:Pswd><xsl:value-of select="$encdata"/></max:Pswd>

In other words, you don't always need to use <xsl:element/> if just coding the desired output is sufficient.

As the max namespace is the same in the wanted result (isn't a different one), you only need to perform this short and simple transformation :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:max="http://mynamespace/C">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pEncrypted" select="'jksdguasidgeiruh'"/>

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

 <xsl:template match="max:CustID/text()">
  <xsl:value-of select="$pEncrypted"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document (corrected to be made well-formed!):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t1="http://mynamespace/A"
xmlns:top="http://mynamespace/B"
xmlns:max="http://mynamespace/C">
    <soapenv:Body>
        <t1:Creditcard>
            <top:AutoPayenroll>
                <top:CustomerId>
                    <max:CustName>Taylor</max:CustName>
                    <max:CustID>1234</max:CustID>
                </top:CustomerId>
            </top:AutoPayenroll>
        </t1:Creditcard>
    </soapenv:Body>
</soapenv:Envelope>

the wanted, correct result is produced :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:t1="http://mynamespace/A"
 xmlns:top="http://mynamespace/B"
 xmlns:max="http://mynamespace/C">
   <soapenv:Body>
      <t1:Creditcard>
         <top:AutoPayenroll>
            <top:CustomerId>
               <max:CustName>Taylor</max:CustName>
               <max:CustID>jksdguasidgeiruh</max:CustID>
            </top:CustomerId>
         </top:AutoPayenroll>
      </t1:Creditcard>
   </soapenv:Body>
</soapenv:Envelope>

Explanation :

Proper use and override of the identity rule/template .

Update :

The OP indicates in a comment that the namespaces can be different on each response and aren't known in advance.

Here is the same solution, slightly modified to accomodate for this specification change;

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pEncrypted" select="'jksdguasidgeiruh'"/>

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

 <xsl:template match="*[local-name()='CustID']/text()">
  <xsl:value-of select="$pEncrypted"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied to the same XML document (above), the same correct, wanted result is produced :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t1="http://mynamespace/A" xmlns:top="http://mynamespace/B" xmlns:max="http://mynamespace/C">
   <soapenv:Body>
      <t1:Creditcard>
         <top:AutoPayenroll>
            <top:CustomerId>
               <max:CustName>Taylor</max:CustName>
               <max:CustID>jksdguasidgeiruh</max:CustID>
            </top:CustomerId>
         </top:AutoPayenroll>
      </t1:Creditcard>
   </soapenv:Body>
</soapenv:Envelope>

Or more safely , (the OP stated in another comment that the namespace-uri s are the same and only the prefixes change):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pEncrypted" select="'jksdguasidgeiruh'"/>

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

 <xsl:template match=
 "*[local-name()='CustID' and namespace-uri()='http://mynamespace/C']/text()">
  <xsl:value-of select="$pEncrypted"/>
 </xsl:template>
</xsl:stylesheet>

You can't use xpath in defining your element name like that. The below will replace CustId with Pswd in the max=http://mynamespace/C namespace, using the identity template to copy everything else:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:max="http://mynamespace/C"
                >
    <xsl:output method="xml" indent="yes"/>

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

    <!--Replace CustID with Pswd-->
    <xsl:template match="*[local-name()='CustID']">
        <xsl:variable name="cleartxt" select="./text()"/>
        <!--got this encrypted data from my internal code-->
        <xsl:variable name="encdata" select="'jksdguasidgeiruh'"/>
        <xsl:element name="max:Pswd">
            <xsl:value-of select="$encdata"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM