1
<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>      
3
  • 1
    I worked on your formatting some. Can you edit your question and fix the rest of it? It will be a lot easier for us to read and understand. Commented Sep 26, 2012 at 3:04
  • Also your "response should be" says you want to output the <max:CustID> with the encrypted text content, but your XSLT says you want to output <Pswd> with that content. Which do you want? Commented Sep 26, 2012 at 3:08
  • Please do what @LarsH wrote. We would like to have the questions on the site be properly formatted so they are of use. Commented Sep 26, 2012 at 8:40

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I know we can use max namespace, but everytime the namespace prefix is changing sometimes ns1 and sometimes ns2 like that. So I want to use what ever it was passed in that request without hardcoding in xsl
I don't know why you'd want to preserve arbitrarily assigned namespace prefixes. Still - these extra requirements have allowed Dimitre to show some nice techniques.
1

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>

4 Comments

Yes agreed, but everytime the namespace prefix is changing sometimes ns1 and sometimes ns2 like that. So I want to use what ever it was passed in that request without hardcoding in xsl
@user1698404, It isn't clear from your comment what is changing to what -- please, explain.
the namespaces t1, top, max they are changing everytime. I mean namespace urls are same but the prefixes are different
@user1698404, Updated once again -- to cover your comment that the namespace-uris don't change.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.