xml - XSLT 将元素从源复制到 CDATA 部分

标签 xml xslt soap namespaces cdata

我正在将一个简单的 SOAP XML 消息转换为一个更扩展的 SOAP XML 消息。我几乎可以正常工作了,但我无法解决最后两个问题。我的问题是:

  1. 元素之后的所有元素都应该在 CDATA 部分中。我尝试使用“cdata-section-elements”,但无法正常工作。
  2. 元素应该是这样的

我的源 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>

最佳答案

以下是否可行?

<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:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
                <xsl:apply-templates select="child::node()"/>
                <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
            </xsl:element>
            <xsl:element name="cais:commandName" namespace="http://www.ortec.com/CAIS">Import</xsl:element>
        </xsl:element>
    </xsl:element>
</xsl:template>

当我以这种方式修改 XSLT 并在您的样本输入上运行它时,我得到:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
  <s:Header><a:Action s:mustUnderstand="1" xmlns:a="http://www.w3.org/2005/08/addressing">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 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>

namespace 声明仍然存在,但这并不重要。

如果你真的想去掉 <test> 上的命名空间声明,您可以将您的身份模板替换为:

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

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

我已经验证这似乎有效。

另外,如果您提前知道元素的前缀和名称,则无需使用 xsl:elementnamespace属性。如果您在 xsl:spreadsheet 中声明 namespace 元素:

<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/" 
                   xmlns:s="http://www.w3.org/2003/05/soap-envelope"
                   xmlns:cais="http://www.ortec.com/CAIS"
                   exclude-result-prefixes="fn xs SOAP-ENV cais">

那么你可以这样做:

<xsl:template match="SOAP-ENV:Body">
    <xsl:element name="s:{local-name()}">
        <cais:SendMessage>
            <cais:message>
                <!-- copy the rest -->
                <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
                <xsl:apply-templates select="child::node()"/>
                <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
            </cais:message>
            <cais:commandName>Import</cais:commandName>
        </cais:SendMessage>
    </xsl:element>
</xsl:template>

关于xml - XSLT 将元素从源复制到 CDATA 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851215/

相关文章:

javascript - 如何使用条件连接字符串?

xml - Xpath:比较十进制类型的节点属性

xml - 将转换后的 xml 保存为 html

javascript - 从 Node.js 多次调用 Web 服务

php - 无法在 Debian 9 上安装 php7.4-soap

web-services - 向Web服务添加方法:旧客户端是否需要更新Web引用?

mysql - 在mysql数据库中使用ibatis添加新人

xml - XQuery 过滤动态行列表

XSLT 属性与命名空间匹配

xml - 为什么 XSLT disable-output-escaping 没有在 Firefox 中实现?