1

I am facing an issue in renaming a xml tag using XSLT. Below is my input xml.

Original XML:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:Response xmlns:ns="http://demo.test.classes.com">
   <ns:return>
      <ns:person>
         <ns:personName></ns:personName>
         <ns:personAge></ns:personAge>
         <ns:personAddress>
            <ns:addressType>official</ns:addressType>
            <ns:addressLine1>official address line 1</ns:addressLine1>
         </ns:personAddress>
         <ns:personAddress>
            <ns:addressType>residence</ns:addressType>
            <ns:addressLine1>residence address line 1</ns:addressLine1>
         </ns:personAddress>         
      </ns:person>
   </ns:return>
</ns:Response>
</soapenv:Body>
</soapenv:Envelope>  

Expected XML after transformation: This is the final xml which I am looking for!!!

<MyResponse>
      <person>
         <personName></personName>
         <personAge></personAge>
         <personAddress>
            <addressType>official</addressType>
            <addressLine1>official address line 1</addressLine1>
         </personAddress>
         <personAddress>
            <addressType>residence</addressType>
            <addressLine1>residence address line 1</addressLine1>
         </personAddress>         
      </person>
</MyResponse>  

This is the XSLT, I am using now. But this is not generating the xml which I need. The issue is, if I don't include the template match="ns:Response", the generated xml is similar to above xml except the root tag being "Response" and it perfectly matching my need. But when I introduce the match="ns:Response" the our xml is not formatted as xml, and the generated xml contain the namespace xmlns:ns="http://demo.test.classes.com" beside the "MyResponse" tag. please let me know what should be modified in the below xslt?

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/|comment()|processing-instruction()">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
      <xsl:value-of select="." />
    </xsl:attribute>
    </xsl:template>

    <xsl:template xmlns:ns="http://demo.test.classes.com" match="ns:return">
    <xsl:apply-templates/>
    </xsl:template>

    <xsl:template xmlns:ns="http://demo.test.classes.com" match="ns:Response">
    <MyResponse><xsl:apply-templates select="node()|@*"/></MyResponse>
    </xsl:template>

</xsl:stylesheet>

2 Answers 2

1

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://demo.test.classes.com" exclude-result-prefixes="x">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="x:Response">
  <MyResponse>
   <xsl:apply-templates/>
  </MyResponse>
 </xsl:template>

 <xsl:template match="x:Response/*//*">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:Response xmlns:ns="http://demo.test.classes.com">
   <ns:return>
      <ns:person>
         <ns:personName></ns:personName>
         <ns:personAge></ns:personAge>
         <ns:personAddress>
            <ns:addressType>official</ns:addressType>
            <ns:addressLine1>official address line 1</ns:addressLine1>
         </ns:personAddress>
         <ns:personAddress>
            <ns:addressType>residence</ns:addressType>
            <ns:addressLine1>residence address line 1</ns:addressLine1>
         </ns:personAddress>
      </ns:person>
   </ns:return>
</ns:Response>
</soapenv:Body>
</soapenv:Envelope>

produces the wanted, correct result:

<MyResponse>
    <person>
        <personName/>
        <personAge/>
        <personAddress>
            <addressType>official</addressType>
            <addressLine1>official address line 1</addressLine1>
        </personAddress>
        <personAddress>
            <addressType>residence</addressType>
            <addressLine1>residence address line 1</addressLine1>
        </personAddress>
    </person>
</MyResponse>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Dimitre for the response.
0

Here is another one. It is a tweak on my answer to your first question. It more verbose than Dimitre's (3 templates instead of 2), but to compensate it may be more efficient as the match conditions are very simple.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
    xmlns:ns="http://demo.test.classes.com"
    exclude-result-prefixes="xsl soapenv ns">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="*">
 <xsl:element name="{local-name()}">
  <xsl:apply-templates />
 </xsl:element>
</xsl:template>

<xsl:template match="ns:return">
  <MyResponse>
   <xsl:apply-templates/>
  </MyResponse>
</xsl:template>

<xsl:template match="soapenv:Envelope|soapenv:Body">
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

3 Comments

Thanks Sean, I modified little bit to make it work the way I wanted. But your suggestion helped me to fix the issue. <xsl:template xmlns:ns="demo.test.classes.com" match="ns:return"> <xsl:apply-templates/> </xsl:template> <xsl:template xmlns:ns="demo.test.classes.com" match="ns:Response"> <MyResponse> <xsl:apply-templates/> </MyResponse> </xsl:template>
Sean I have posted one more question on wsdl, could you pls suggested an expect to provide a solution for my question.
Appreciate if you could check with someone for a solution to this question: stackoverflow.com/questions/12278768/…

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.