1

I have got stuck with minor change for XSLT, because i would like to replace the Top root node with different name. I have tried with below XSLT to add new root, but unable to delete the initial root of input xml. Can someone please let me know what i am missing here. Thank you.

Below is the XSLT code tried.

                     <?xml version='1.0' encoding='utf-8'?>
                    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                        <xsl:output method="xml" encoding="UTF-8"/>

                        <xsl:template match="root">
                            <ns:NewParent xmlns:ns="http://test.com/user">
                                <xsl:copy-of select="." />
                            </ns:NewParent>
                        </xsl:template>

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

Input XML :

                    <root>
                        <root>
                            <a>TestA</a>
                            <b>Testb</b>
                            <c>Testc</c>
                        </root>
                        <root>
                            <a>TestA1</a>
                            <b>Testb1</b>
                            <l>Testl1</l>
                        </root>
                        <root>
                            <a>TestA12</a>
                            <b>Testb12</b>
                            <l>Testl2</l>
                        </root>
                    </root>

Output/Desired XML :

                    <ns:NewParent xmlns:ns="http://test.com/user">
                        <root>
                            <a>TestA</a>
                            <b>Testb</b>
                            <c>Testc</c>
                        </root>
                        <root>
                            <a>TestA1</a>
                            <b>Testb1</b>
                            <l>Testl1</l>
                        </root>
                        <root>
                            <a>TestA12</a>
                            <b>Testb12</b>
                            <l>Testl2</l>
                        </root>
                    </ns:NewParent>

1 Answer 1

2

The template matching root does <xsl:copy-of select="." /> which copies the node itself, as well as all children. You should do <xsl:copy-of select="node()" /> instead.

<xsl:template match="root">
    <ns:NewParent xmlns:ns="http://test.com/user">
        <xsl:copy-of select="node()" />
    </ns:NewParent>
</xsl:template>

Note that you wouldn't need the identity template in this case. Just this template would do. If you did want to use the identity template, or if you needed to transform other nodes, use xsl:apply-templates instead

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

    <xsl:template match="/root">
        <ns:NewParent xmlns:ns="http://test.com/user">
            <xsl:apply-templates />
        </ns:NewParent>
    </xsl:template>

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

Note how the template now matches /root so that it only matches the top level root element, and not its children.

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

1 Comment

Thank you very much Tim :) got it. Appreciate your help.

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.