如何在包含<的xml上编写xslt? >而不是'而不是'而不是'和'>'

时间:2018-05-01 07:36:21

标签: xml xslt

例如,我正在调用一个Web服务,它给出了以下响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header/>
    <soapenv:Body>
        <executeResponse>
            <executeReturn>
                &lt;Message&gt;
                    &lt;Body&gt;
                        &lt;Clients&gt;
                            &lt;Client&gt;
                                &lt;ClientID&gt;C000001&lt;/ClientID&gt;
                            &lt;/Client&gt;
                            &lt;Client&gt;
                                &lt;ClientID&gt;C000002&lt;/ClientID&gt;
                            &lt;/Client&gt;
                        &lt;/Clients&gt;
                    &lt;/Body&gt;
                &lt;/Message&gt;
            </executeReturn>
        <executeResponse>
    </soapenv:Body>
</soapenv:Envelope>

如何编写XSLT以便我可以将响应转换为:

<Customers>
    <Customer>
        <CustomerID>C000001<CustomerID>
        <CustomerID>C000002<CustomerID>
    <Customer>
<Customers>

1 个答案:

答案 0 :(得分:2)

如果您可以访问像Saxon 9.8这样的XSLT 3处理器,您可以使用parse-xml函数将转义的标记解析为XML节点,然后您可以将它们转换为

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:mode on-no-match="text-only-copy"/>

  <xsl:template match="executeReturn">
      <xsl:apply-templates select="parse-xml(.)"/>
  </xsl:template>

  <xsl:template match="Clients">
      <Customers>
          <Customer>
              <xsl:apply-templates/>
          </Customer>
      </Customers>
  </xsl:template>

  <xsl:template match="ClientID">
      <CustomerID>
          <xsl:apply-templates/>
      </CustomerID>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/eiZQaF2的输入转换为

<?xml version="1.0" encoding="UTF-8"?>
<Customers>
   <Customer>
      <CustomerID>C000001</CustomerID>
      <CustomerID>C000002</CustomerID>
   </Customer>
</Customers>

使用早期版本的XSLT,您需要检查您的处理器是否提供扩展功能或允许您实现一个扩展功能,或者您需要编写两个XSLT样式表,其中第一个使用disable-output-escaping输出转义标记,然后第二个输出转换第一个输出。