Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT 1.0 Remove xmlns="" from element

I have this XML message:

<Orders xmlns="http://AU.InputOrders">
<Order>
<OrderRef>D04004451</OrderRef>
<ABN>9780571244461</ABN>
<OrderQty>1</OrderQty>
</Order>
<Order>
<OrderRef>D04004451</OrderRef>
<ABN>9780571254057</ABN>
<OrderQty>1</OrderQty>
</Order>
<Order>
<OrderRef>D04004452</OrderRef>
<ABN>9781864489545</ABN>
<OrderQty>1</OrderQty>
</Order>
</Orders>

and this XSLT 1.0:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns0="http://AU.InputOrders">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>

<xsl:template match="/">
<xsl:element name="Orders" namespace="http://AU.OutputOrders">
<xsl:apply-templates select="ns0:Orders/ns0:Order[1]" />
</xsl:element>
</xsl:template>

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

</xsl:stylesheet>

Above XSLT gives me the following output:

<Orders xmlns="http://AU.OutputOrders"><Order xmlns="">
<OrderRef>D04004451</OrderRef>
<ABN>9780571244461</ABN>
<OrderQty>1</OrderQty>
</Order></Orders>

Which is almost what I need, but I don't want that xlmns="" after Order node.

What I want is this:

<Orders xmlns="http://AU.OutputOrders"><Order>
<OrderRef>D04004451</OrderRef>
<ABN>9780571244461</ABN>
<OrderQty>1</OrderQty>
</Order></Orders>

Can someone help me with this? Thanks.

like image 411
g_eduard Avatar asked Jul 12 '13 00:07

g_eduard


1 Answers

You need to output the other elements (matched with *) in the same namespace:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ns0="http://AU.InputOrders" xmlns="http://AU.OutputOrders">
    <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>

    <xsl:template match="/*">
        <xsl:element name="{name()}" namespace="http://AU.OutputOrders">
            <xsl:apply-templates select="ns0:Order[1]"/>            
        </xsl:element>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{name()}" namespace="http://AU.OutputOrders">
            <xsl:apply-templates select="node()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
like image 75
Daniel Haley Avatar answered Sep 29 '22 01:09

Daniel Haley