Ven
Ven

Reputation: 85

Adding Soapenv,header,bodz with prefixes to XMLtags using XSLT

I have below requirements to suffice for one XML file.

  1. removing the Main node
  2. removing the existing namespace
  3. Adding soapenv envelope, header and body tags.
  4. adding prefixes with namespaces to parent node and other nodes.

So far i have succeeded with first two requirements, but not the last two. Can someone please help me with the below requirement. thank you in advance.

Below is my input xml:

        <?xml version="1.0" encoding="UTF-8"?>
            <ns0:MainNode xmlns:ns0="http://test/system">
                <MainResponse>
                    <Header>
                        <Value>12AB</Value>
                        <QTY>10000</QTY>
                        <DetailList>
                            <ActualValue>1</ActualValue>
                            <ActualValue>2</ActualValue>
                            <ActualValue>3</ActualValue>
                        </DetailList>
                    </Header>
                </MainResponse>
            </ns0:MainNode>

Target XML Should be :

            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
                <soapenv:Header/>
                <soapenv:Body>
                    <a123:MainResponse xmlns:a123="http://www.ibm.com/test/One/Type">
                        <b345:Header xmlns:b345="http://www.ibm.com/test/One/DeType">
                            <b345:Value>12AB</b345:Value>
                            <b345:QTY>10000</b345:QTY>
                            <b345:DetailList>
                                <b345:ActualValue>1</b345:ActualValue>
                                <b345:ActualValue>2</b345:ActualValue>
                                <b345:ActualValue>3</b345:ActualValue>
                            </b345:DetailList>
                        </b345:Header>
                    </a123:MainResponse>
                </soapenv:Body>
            </soapenv:Envelope>

Tried XSLT Code:

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

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


            <!-- Removing first node -->
            <xsl:template match="/">
                <xsl:apply-templates select="*/*" />
            </xsl:template>

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


        </xsl:stylesheet>

Upvotes: 0

Views: 479

Answers (1)

Aniket V
Aniket V

Reputation: 3247

Please try the XSLT below.

You need to do selective matching of the templates and accordingly add the namespace and prefix.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns0="http://test/system" exclude-result-prefixes="ns0">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="ns0:MainNode">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
            <soapenv:Header />
            <soapenv:Body>
                <xsl:apply-templates />
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>

     <xsl:template match="MainResponse">
        <xsl:element name="a123:{local-name()}" namespace="http://www.ibm.com/test/One/Type">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>

     <xsl:template match="Header | Header//*">
        <xsl:element name="b345:{local-name()}" namespace="http://www.ibm.com/test/One/DeType">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <soapenv:Header />
    <soapenv:Body>
        <a123:MainResponse xmlns:a123="http://www.ibm.com/test/One/Type">
            <b345:Header xmlns:b345="http://www.ibm.com/test/One/DeType">
                <b345:Value>12AB</b345:Value>
                <b345:QTY>10000</b345:QTY>
                <b345:DetailList>
                    <b345:ActualValue>1</b345:ActualValue>
                    <b345:ActualValue>2</b345:ActualValue>
                    <b345:ActualValue>3</b345:ActualValue>
                </b345:DetailList>
            </b345:Header>
        </a123:MainResponse>
    </soapenv:Body>
</soapenv:Envelope>

Upvotes: 1

Related Questions