XSLT - 将命名空间添加到节点XML

时间:2016-11-11 17:26:43

标签: xml xslt soap xml-namespaces

我只有XML

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

我使用XSLT FinalResponse 纳入CDATA(字符串):

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

然后我再使用其他XSLTXML转换为SOAP,并使用我想要的命名空间:

<?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>

我需要将此标记/命名空间xmlns="http://tempuri.org/"添加到RequestResponse节点,但该节点如下:

<?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>

如何仅将该元素添加到该节点?

1 个答案:

答案 0 :(得分:2)

XSLT样式表(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>

变换

<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>

<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>