0

I have a simply XML:

<RequestResponse>
    <RequestResult>
        <FinalResponse>
            <Message>Request inserted successfully.</Message>
            <Response>true</Response>
        </FinalResponse>
    </RequestResult>
</RequestResponse>

I use a XSLT to include FinalResponse into a CDATA (String):

<RequestResponse>    
    <RequestResult>
        <![CDATA[<FinalResponse>      
                <Message>Request inserted successfully.</Message>
                <Response>true</Response>   
        </FinalResponse>]]>
    </RequestResult>   
</RequestResponse>

Then I use another XSLT more to convert the XML to SOAP with the namespace I want:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <RequestResponse>
            <RequestResult>
                <![CDATA[
                <FinalResponse>
                <Message>Request inserted successfully.</Message>
                <Response>true</Response>
                </FinalResponse>
                ]]>
            </RequestResult>
        </RequestResponse>
    </s:Body>
</s:Envelope>

I need to add this tag/namespace: xmlns="http://tempuri.org/" to the RequestResponse node, but only that node like this:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <RequestResponse xmlns="http://tempuri.org/">
            <RequestResult>
                <![CDATA[
                <FinalResponse>
                <Message>Request inserted successfully.</Message>
                <Response>true</Response>
                </FinalResponse>
                ]]>
            </RequestResult>
        </RequestResponse>
    </s:Body>
</s:Envelope>

How can I add that element only to that node?

1
  • You misunderstand how namespaces work. In your output, the RequestResult element inherits the default namespace declared for its parent RequestResponse element. You must place both elements in the same namespace, not only the parent. Commented Nov 11, 2016 at 20:56

1 Answer 1

2

The XSLT stylesheet (http://xsltransform.net/bwdws2)

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output cdata-section-elements="RequestResult" xmlns="http://tempuri.org/"/>

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

    <xsl:template match="RequestResponse | RequestResponse//*">
        <xsl:element name="{name()}" namespace="http://tempuri.org/">
          <xsl:apply-templates select="@* | node()"/>            
        </xsl:element>
    </xsl:template>

</xsl:transform>

transforms

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <RequestResponse>
            <RequestResult>
                <![CDATA[
                <FinalResponse>
                <Message>Request inserted successfully.</Message>
                <Response>true</Response>
                </FinalResponse>
                ]]>
            </RequestResult>
        </RequestResponse>
    </s:Body>
</s:Envelope>

into

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <RequestResponse xmlns="http://tempuri.org/">
            <RequestResult><![CDATA[

                <FinalResponse>
                <Message>Request inserted successfully.</Message>
                <Response>true</Response>
                </FinalResponse>

            ]]></RequestResult>
        </RequestResponse>
    </s:Body>
</s:Envelope>
Sign up to request clarification or add additional context in comments.

2 Comments

I tries with your xsl transformation, but it doesn't add the CDATA. Please, read my answer below
I finally got it, changing the order of transformation. Thank you very much

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.