1

I was trying to convert the below xml that has xml namespace aliases on all tags.

Input:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:Header xmlns:ns1="abc.com">
    <ns1:Child1>
        <ns1:Child11>a</ns1:Child11>
        <ns1:Child12>b</ns1:Child12>
        <ns1:Child13>
            <ns1:Picks>
                <ns1:pick1>1</ns1:pick1>
                <ns1:pick2>2</ns1:pick2>
            </ns1:Picks>
        </ns1:Child13>
    </ns1:Child1>
</ns1:Header>

Desired Output:

<?xml version="1.0" encoding="UTF-8"?>
<Header xmlns:ns1="abc.com">
    <Child1>
        <Child11>a</Child11>
        <Child12>b</Child12>
        <Child13>
            <Picks>
                <pick1>1</pick1>
                <pick2>2</pick2>
            </Picks>
        </Child13>
    </Child1>
</Header>

Is there any easier out of the box XSLT ways to accomplish this?

1 Answer 1

2

If I interpret your question correctly, you want to change the namespaces of the child elements of Header.
This can be achieved with the following templates:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="abc.com" >
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="*">                <!-- Removes all ns1 namespaces from the child elements -->
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node()|@*" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="ns1:Header">       <!-- Removes the ns1 namespace from the Header element but keeps an xmlns:ns1 definition -->
    <Header xmlns:ns1="abc.com">
        <xsl:apply-templates select="node()|@*" />
    </Header>
  </xsl:template>

</xsl:stylesheet>

Its output is:

<?xml version="1.0"?>
<Header xmlns:ns1="abc.com">
    <Child1>
        <Child11>a</Child11>
        <Child12>b</Child12>
        <Child13>
            <Picks>
                <pick1>1</pick1>
                <pick2>2</pick2>
            </Picks>
        </Child13>
    </Child1>
</Header>

If necessary, add an identity template to copy missing elements.


If you want to change the default namespace of all elements instead, this would simplify things to this one template:

<xsl:template match="*">             <!-- Changes the default namespaces of all elements -->
  <xsl:element name="{local-name()}" namespace="abc.com">
    <xsl:apply-templates select="node()|@*" />
  </xsl:element>
</xsl:template>

In this case the output would be:

<?xml version="1.0"?>
<Header xmlns="abc.com">
    <Child1>
        <Child11>a</Child11>
        <Child12>b</Child12>
        <Child13>
            <Picks>
                <pick1>1</pick1>
                <pick2>2</pick2>
            </Picks>
        </Child13>
    </Child1>
</Header>
Sign up to request clarification or add additional context in comments.

5 Comments

My guess would be the OP just wants to switch from a namespace with a prefix to a default namespace, for purely optical reasons, and that his second code sample is not actually correct.
Thank you very much. This is exactly what I needed.
@Fkhader: Just for completeness I added a second possibility realizing Tomalak's objection to the answer.
@zx485 : if Header tag had an additional attribute 'tag1' as below, it throws me off: <Header xmlns:ns1="abc.com" tag1 = "test">
@Fkhader: Adding the identity template should fix this.

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.