将XML转换为字符串 - XSL

时间:2015-10-27 17:19:26

标签: xml xslt

我有这样的XML -

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <fis:AcctAssnSvcRqst>    
            <abcd/>
            <efgh/>   
        </fis:AcctAssnSvcRqst>   
    </soapenv:Body>
</soapenv:Envelope>

我想要输出如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gc="http://oracle.com/APIService.xsd">
    <soapenv:Header/>
    <soapenv:Body>
        <gc:APIService dateTimeTagFormat="xsd:strict">
            <gc:input>
                <gc:SoapEnvelope>
                     <![CDATA[<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                         <soapenv:Body>
                             <fis:AcctAssnSvcRqst>
                                 <abcd/>
                                 <efgh/>
                             </fis:AcctAssnSvcRqst>
                         </soapenv:Body>
                     </soapenv:Envelope>]]>
               </gc:SoapEnvelope>
            </gc:input>
        </gc:GC-FISAPIService>
    </soapenv:Body>
</soapenv:Envelope>

我想将XML转换为字符串并将其放在XSL中,并将响应从String转换回XML。我尝试了多种方法,但没有一种方法有效。

1 个答案:

答案 0 :(得分:0)

使用Saxon 9.6,您可以使用

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:gc="http://oracle.com/APIService.xsd"
  exclude-result-prefixes="xs"
  version="3.0">

<xsl:output method="xml" cdata-section-elements="gc:SoapEnvelope"/>

<xsl:variable name="ser1">
<output:serialization-parameters xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization">
  <output:omit-xml-declaration value="yes"/>
</output:serialization-parameters>
</xsl:variable>

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

<xsl:template match="soapenv:Envelope">
  <soapenv:Envelope>
    <xsl:apply-templates/>
  </soapenv:Envelope>
</xsl:template>

<xsl:template match="soapenv:Body">
  <xsl:copy>
    <gc:APIService dateTimeTagFormat="xsd:strict">
        <gc:input>
            <gc:SoapEnvelope>
               <xsl:value-of select="serialize(/*, $ser1/*)"/>
             </gc:SoapEnvelope>
        </gc:input>
    </gc:APIService>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

转换

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fis="http://example.com/fis">
    <soapenv:Body>
        <fis:AcctAssnSvcRqst>

            <abcd/>
            <efgh/>

        </fis:AcctAssnSvcRqst>

    </soapenv:Body>
</soapenv:Envelope>

进入结果

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gc="http://oracle.com/APIService.xsd">
    <soapenv:Body xmlns:fis="http://example.com/fis"><gc:APIService dateTimeTagFormat="xsd:strict"><gc:input><gc:SoapEnvelope><![CDATA[<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fis="http://example.com/fis">
    <soapenv:Body>
        <fis:AcctAssnSvcRqst>

            <abcd/>
            <efgh/>

        </fis:AcctAssnSvcRqst>

    </soapenv:Body>
</soapenv:Envelope>]]></gc:SoapEnvelope></gc:input></gc:APIService></soapenv:Body>
</soapenv:Envelope>