Ven
Ven

Reputation: 85

Removal of SourceNamespace by appending new tags to XML using XSLT

I am having an xml as source and would like to do below changes. 1. Removing first root tag 2. Add new soapenv root tags 3. Remove the namespace from source.

I am able to add the new tags, but removal is not getting done with XSLT1.0 version.(I have achieved it with XSLT2.0, but my XSLT processor is 1.0). I know I am missing some basic logic here. Can someone please help me out. Thank you very much.

Source XML:

 <?xml version="1.0" encoding="UTF-8"?>
                <ns0:Header xmlns:ns0="http://xyz987.com">
                    <Main>
                        <Parent2>
                            <status>12</status>
                            <status_txt>Helo</status_text>
                        </Parent2>
                        <Parent3>
                            <Child1>112</Child1>
                            <Child2>Hai</Child2>
                        </Parent3>
                        <Parent4>
                            <Child3>Valley</Child3>
                            <Parent5>
                                <Child7>Kind</Child7>
                                <Child8>Pls</Child8>
                            </Parent5>
                        </Parent4>
                    </Main>
                </ns0:Header>

Target XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                    <soapenv:Header/>
                    <soapenv:Body>
                        <Main>
                            <Parent2>
                                <status>12</status>
                                <status_txt>Helo</status_txt>
                            </Parent2>
                            <Parent3>
                                <Child1>112</Child1>
                                <Child2>Hai</Child2>
                            </Parent3>
                            <Parent4>
                                <Child3>Valley</Child3>
                                <Parent5>
                                    <Child7>Kind</Child7>
                                    <Child8>Pls</Child8>
                                </Parent5>
                            </Parent4>
                        </Main>
                    </soapenv:Body>
                </soapenv:Envelope>

XSLT 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" omit-xml-declaration="yes"/>

                        <xsl:template match="*">

                            <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
                                <soapenv:Header/>
                                <soapenv:Body>

                                    <xsl:copy-of select="*" copy-namespaces="no"/>

                                </soapenv:Body>
                            </soapenv:Envelope>

                        </xsl:template>
                    </xsl:stylesheet>

Upvotes: 0

Views: 30

Answers (1)

Tim C
Tim C

Reputation: 70618

The use of copy-namespaces is not supported in XSLT 1.0. You could infact, just do <xsl:copy-of select="*" /> but you would end up with XML like this..

<Main xmlns:ns0="http://xyz987.com">

Having an unused namespace declaration like this should not actually cause a problem. The Main element and all its descendants are not actually in that namespace. However, if you did want to get rid of it, you would to use xsl:apply-templates instead, and have a template to create new elements that don't have any namespace declarations attached

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

You would need to change your main template to match /* in this case, to avoid a conflict. Try this XSLT

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

    <xsl:template match="/*">
        <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:apply-templates />
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>

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

Upvotes: 2

Related Questions