3

我正在尝试找到可以执行以下操作的 XSLT。

我正在尝试将 XML 消息包装在快速肥皂信封 (xml) 中。源 XML 不是一成不变的,所以 XSLT 不应该关心 XML 是什么。它确实需要从 XML 顶部剥离 XML 声明。

例子:

<?xml version="1.0"?>
<foo>
    <bar />
</foo>

转换为:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:hl7-org:v3">
    <soapenv:Header/>
    <soapenv:Body>
          <foo>
            <bar />
          </foo>
    </soapenv:Body>
</soapenv:Envelope>  

示例 2:

<lady>
  <and>
   <the>
    <tramp />
   </the>
  </and>
</lady>



<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:hl7-org:v3">
    <soapenv:Header/>
    <soapenv:Body>
         <lady>
           <and>
              <the>
                  <tramp />
             </the>
           </and>
         </lady>
    </soapenv:Body>
</soapenv:Envelope>  
4

2 回答 2

2

这个 XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="*">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns="urn:hl7-org:v3">
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:copy-of select="/*"/>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

输出此 XML:

<soapenv:Envelope xmlns="urn:hl7-org:v3" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <lady xmlns="">
         <and>
            <the>
               <tramp/>
            </the>
         </and>
      </lady>
   </soapenv:Body>
</soapenv:Envelope>
于 2013-04-16T23:28:11.057 回答
2

您可以使用它(类似于 Phoenixblade9 的答案,但不使用xsl:element

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:hl7-org:v3">
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:copy-of select="*"/>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>

</xsl:stylesheet>
于 2013-04-16T23:41:09.607 回答