Akhil Sood
Akhil Sood

Reputation: 3

How to remove xmlns="" with xslt mapping

I want to transform following xml

<IRheader>
    <Keys>
        <Key Type="TaxOfficeNumber">33</Key>
        <Key Type="TaxOfficeReference">33345</Key>
    </Keys>
    <PeriodEnd>2017-02-28</PeriodEnd>
    <Sender>Company</Sender>
</IRheader>

into

<IRheader>
    <Keys>
        <Key Type="TaxOfficeNumber">33</Key>
        <Key Type="TaxOfficeReference">33345</Key>
    </Keys>
    <PeriodEnd>2017-02-28</PeriodEnd>
    <IRmark Type="generic"></IRmark>
    <Sender>Company</Sender>
</IRheader>

I am using following xsl code to transform

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

    <xsl:template match="@*|node()" name="t1">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[local-name()='PeriodEnd']">
        <xsl:call-template name="t1" />
        <xsl:element name="IRmark">
            <xsl:attribute name="Type"><xsl:text>generic</xsl:text></xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

After that i am getting

<IRheader>
    <Keys>
        <Key Type="TaxOfficeNumber">33</Key>
        <Key Type="TaxOfficeReference">33345</Key>
    </Keys>
    <PeriodEnd>2017-02-28</PeriodEnd>
    <IRmark xmlns="" Type="generic"></IRmark>
    <Sender>Company</Sender>
</IRheader>

Kindly suggest how to remove unwanted xmlns=""

Upvotes: 0

Views: 380

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

If you put the elements in the right namespace then the namespace declarations will generally take care of themselves. This problem usually occurs because the parent element is in a namespace but IRMark is not. If you generate IRMark in the desired namespace then the namespace declaration will usually disappear.

In your example the containing elements don't appear to be in a namespace. As your post shows no evidence that you understand namespaces, I'm wondering if you removed them from the code you have shown us because you didn't realise they mattered? If that's not the case, then I can't see why the xmlns="" undeclaration is present, and I would want to know which XSLT processor you are using.

Upvotes: 1

Related Questions