我只有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>
然后我再使用其他XSLT
将XML
转换为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>
如何仅将该元素添加到该节点?
答案 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>