4

I need to extract an XML out of an envelope. But I am not able to get my intended output. I need to get rid of the namespaces in my output.

My Input:

<ns1:input xmlns:ns1="http://mysapmle.org/" xmlns="http://othersample.org/">
        <sample>
            <inner tc="5">Test</inner>
            <routine>Always</routine>
        </sample>
</ns1:input>

My Expected Output:

<sample>
    <inner tc="5">Test</inner>
    <routine>Always</routine>
</sample>

My Actual Output:

    <sample xmlns="http://othersample.org/">
            <inner tc="5">Test</inner>
            <routine>Always</routine>
        </sample>

My XSLT:

<xsl:output omit-xml-declaration="yes" indent="yes" />

<xsl:template match="/">
    <xsl:copy copy-namespaces="no">
        <xsl:apply-templates select="//sample" />           
    </xsl:copy>     
</xsl:template> 

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

Please help.

2
  • 1
    That's stripping out tags, not just "removing a namespace." Commented Jan 16, 2013 at 20:22
  • 1
    It's kind of that. I need to include this extracted XML as part of another XML. That is why I cannot have the namespace. Commented Jan 16, 2013 at 20:27

1 Answer 1

2

This should work for stripping out the namespaces:

<xsl:output omit-xml-declaration="yes" indent="yes" />

<xsl:template match="/">
   <xsl:apply-templates select="*/*" />
</xsl:template>

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

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

But I'm somewhat doubtful that you need to be doing this. If the XML you're appending this to is using the same namespace and this is the namespace that this contained XML is supposed to have, it should not matter that this XML has namespace declarations in it.

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

1 Comment

They have a different namespace. That is why I cannot keep this.

Your Answer

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