0

Original Request

<?xml version="1.0" encoding="utf-8"?>
<typ:CustomerResponse xmlns:typ="http://xml.mycomp.com/customer/types">
 <typ:CustomerReturn>
    <typ:Address>
      <typ:state>PA</typ:state>
      <typ:city>Harrisburg</typ:city>
    </typ:Address>
    <typ:User>
       <typ:firstName>test</typ:firstName>
       <typ:lastName>test</typ:lastName>
    </typ:User>
 </typ:CustomerReturn>
</typ:CustomerResponse>

I removed xml namespaces using the xslt below.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes" />

    <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 match="@xsi:nil[.='true']"/>

</xsl:stylesheet>

After running the above transformation on my orginal xml which has namespaces I get the below -

<?xml version="1.0" encoding="utf-8"?>
<CustomerResponse>
 <CustomerReturn>
    <Address>
      <state>PA</state>
      <city>Harrisburg</city>
    </Address>
    <User>
       <firstName>test</firstName>
       <lastName>test</lastName>
    </User>
 </CustomerReturn>
</CustomerResponse>

I want to enhance the existing xslt file to get output similar to below.

Expected Output

  <CustomerResponse>
       <Address>
          <state>PA</state>
          <city>Harrisburg</city>
        </Address>
        <User>
           <firstName>test</firstName>
           <lastName>test</lastName>
        </User>
 </CustomerResponse>
4
  • Your question is not clear. Please show the original input XML and the expected output of the transformation. Jul 27, 2016 at 23:26
  • Hi Michael , added the expected output. Jul 27, 2016 at 23:30
  • 1
    Please show us the original input, too. -- Are you aware that the output you want is not well-formed XML? Jul 27, 2016 at 23:32
  • Hi Michael , updated both the original request and expected one. Thanks for pointing out the error. Jul 27, 2016 at 23:37

1 Answer 1

2

Try it this way?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:typ="http://xml.mycomp.com/customer/types"
exclude-result-prefixes="typ">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="typ:CustomerReturn">
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>
3
  • Thanks works awesome. Thank you very much. Is it possible to explain what is being done? Jul 28, 2016 at 15:09
  • The first template creates a corresponding element in no-namespace for every element in the original XML. It does so recursively, traversing the entire tree from root to leaves. The second template overrides for typ:CustomerReturn: it continues the recursion (by calling <xsl:apply-templates/>), but it skips creating a corresponding element for the current node. Thus children of typ:CustomerReturn move up in the hierarchy and become children of CustomerResponse. -- See also: w3.org/TR/xslt/#section-Processing-Model Jul 28, 2016 at 16:30
  • Thanks a lot , Michael! Jul 28, 2016 at 18:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.