我正在將簡單的SOAP XML消息轉換爲更加擴展的SOAP XML消息。我幾乎可以工作,但我無法解決最後兩個問題。我的問題是:將元素從源複製到CDATA部分的XSLT
- 元素之後的所有元素應該位於CDATA節中。我嘗試使用'cdata-section-elements',但無法使其工作。
- 元素應該是這樣的
我的源XML文件:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<test>
<element1>123</element1>
<element2>123</element2>
</test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" exclude-result-prefixes="fn xs SOAP-ENV">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<!-- Special rule to match the document root only -->
<xsl:template match="/*">
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<xsl:namespace name="a" select="'http://www.w3.org/2005/08/addressing'"/>
<xsl:apply-templates select="@*|node()"/>
</s:Envelope>
</xsl:template>
<!-- Expand soap header -->
<xsl:template match="SOAP-ENV:Header">
<xsl:element name="s:{local-name()}" namespace="http://www.w3.org/2003/05/soap-envelope">
<xsl:element name="a:Action" namespace="http://www.w3.org/2005/08/addressing">
<xsl:attribute name="s:mustUnderstand" namespace="http://www.w3.org/2003/05/soap-envelope">1</xsl:attribute>
<xsl:text>http://www.ortec.com/CAIS/IApplicationIntegrationService/SendMessage</xsl:text>
</xsl:element>
</xsl:element>
</xsl:template>
<!-- Change soap body -->
<xsl:template match="SOAP-ENV:Body">
<xsl:element name="s:{local-name()}" namespace="http://www.w3.org/2003/05/soap-envelope">
<xsl:element name="cais:SendMessage" namespace="http://www.ortec.com/CAIS">
<xsl:element name="cais:message" namespace="http://www.ortec.com/CAIS">
<!-- copy the rest -->
<xsl:apply-templates select="child::node()"/>
</xsl:element>
<xsl:element name="cais:commandName" namespace="http://www.ortec.com/CAIS">Import</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
<!-- template for the copy of the rest -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
錯誤輸出我現在有了這個XSLT得到:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.ortec.com/CAIS/IApplicationIntegrationService/SendMessage</a:Action>
</s:Header>
<s:Body>
<cais:SendMessage xmlns:cais="http://www.ortec.com/CAIS">
<cais:message>
<test xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<element1>123</element1>
<element2>123</element2>
</test>
</cais:message>
<cais:commandName>Import</cais:commandName>
</cais:SendMessage>
</s:Body>
</s:Envelope>
我的期望輸出:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.ortec.com/CAIS/IApplicationIntegrationService/SendMessage</a:Action>
</s:Header>
<s:Body>
<cais:SendMessage xmlns:cais="http://www.ortec.com/CAIS">
<cais:message>
<![CDATA[
<test>
<element1>123</element1>
<element2>123</element2>
</test>
]]>
</cais:message>
<cais:commandName>Import</cais:commandName>
</cais:SendMessage>
</s:Body>
</s:Envelope>
+1,記錄完備的問題!如果只有所有的XSLT問題都包含輸入XML,嘗試的XSLT,期望的輸出和實際輸出。 – LarsH 2013-04-11 15:50:39