将XML转换为字符串XSL



我有这样的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中,然后将响应从字符串转换回XML。我尝试了多种方法,但都不起作用。

使用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>

最新更新