将整个XML文件(包括CDATA)编码到Soap Envelope中

我需要获取
XML的全部内容,包括CDATA标签,并对CDATA标签进行编码,并将整个内容添加到肥皂信封的正文中

<?xml version="1.0" encoding="UTF-8"?><Jobs>
<Job>
<JobAction><![CDATA[Add]]></JobAction>
<ATSJobID><![CDATA[ATSJOBid_SAMPLE123]]></ATSJobID>
<JobTitle><![CDATA[Senior Launch Commander]]></JobTitle>
<JobCity><![CDATA[Jacksonville]]></JobCity>
<JobState><![CDATA[FL]]></JobState>
<JobCountry><![CDATA[US]]></JobCountry>
<JobZipCode><![CDATA[32256]]></JobZipCode>
<JobType><![CDATA[FullTime]]></JobType>
<JobCode><![CDATA[19]]></JobCode>
</Job>
</Jobs>

应该在body标签中显示如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:v2="http://www.smashfly.com/services/simplepost/v2" xmlns:sf="http://schemas.datacontract.org/2004/07/SF.WebHelpers">
  <soapenv:Header>
  </soapenv:Header>
  <soapenv:Body>
    <v2:JobXml><![CDATA[<?xml version="1.0" encoding="UTF-8"?><Jobs>
<Job>
<JobAction><![CDATA[Add]]]]><![CDATA[></JobAction>
<ATSJobID><![CDATA[ATSJOBid_SAMPLE123]]]]><![CDATA[></ATSJobID>
<JobTitle><![CDATA[Senior Launch Commander]]]]><![CDATA[></JobTitle>
<JobCity><![CDATA[Jacksonville]]]]><![CDATA[></JobCity>
<JobState><![CDATA[FL]]]]><![CDATA[></JobState>
<JobCountry><![CDATA[US]]]]><![CDATA[></JobCountry>
<JobZipCode><![CDATA[32256]]]]><![CDATA[></JobZipCode>
<JobType><![CDATA[FullTime]]]]><![CDATA[></JobType>
<JobCode><![CDATA[19]]]]><![CDATA[></JobCode>
</Job>
<Jobs>]]>
</v2:JobXml>
  </soapenv:Body>
</soapenv:Envelope>

请注意,整个Body都包含在自己的CDATA标记中.

我试图一起使用,但它没有逃脱

<?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:output method="xml" cdata-section-elements="Job" />
    <xsl:template match="*">

        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://www.smashfly.com/services/simplepost/v2" xmlns:sf="http://schemas.datacontract.org/2004/07/SF.WebHelpers">
  <soapenv:Header>
    <v2:AuthTicket>
      <sf:_accountNumber></sf:_accountNumber>
      <sf:_clientVersion></sf:_clientVersion>

    </v2:AuthTicket>
  </soapenv:Header>
  <soapenv:Body>
    <v2:JobXml>
               <xsl:copy-of select="/*"/>
    </v2:JobXml>
  </soapenv:Body>
</soapenv:Envelope> 
    </xsl:template>
</xsl:stylesheet>

产生

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:v2="http://www.smashfly.com/services/simplepost/v2"
                  xmlns:sf="http://schemas.datacontract.org/2004/07/SF.WebHelpers">
   <soapenv:Header>
      <v2:AuthTicket>
         <sf:_accountNumber/>
         <sf:_clientVersion/>
      </v2:AuthTicket>
   </soapenv:Header>
   <soapenv:Body>
      <v2:JobXml>
         <Jobs>
            <Job><![CDATA[
]]><JobAction>Add</JobAction><![CDATA[
]]><JobID>ATSJOBid_SAMPLE123</JobID><![CDATA[
]]><JobTitle>Senior Launch Commander</JobTitle><![CDATA[
]]><JobCity>Jacksonville</JobCity><![CDATA[
]]><JobState>FL</JobState><![CDATA[
]]><JobCountry>US</JobCountry><![CDATA[
]]><JobZipCode>32256</JobZipCode><![CDATA[
]]><JobType>FullTime</JobType><![CDATA[
]]><JobCode>19</JobCode><![CDATA[
]]></Job>
         </Jobs>
      </v2:JobXml>
   </soapenv:Body>
</soapenv:Envelope>

最佳答案 我认为XSLT 3.0可以解决该任务,因为它有一个序列化方法:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:v2="http://www.smashfly.com/services/simplepost/v2"
    xmlns:sf="http://schemas.datacontract.org/2004/07/SF.WebHelpers"
    version="3.0">

    <xsl:output cdata-section-elements="v2:JobXml" indent="yes"/>

    <xsl:template match="/">
        <soapenv:Envelope>
            <soapenv:Header>
            </soapenv:Header>
            <soapenv:Body>
                <v2:JobXml>
                    <xsl:value-of select="serialize(., map { 'cdata-section-elements' : Jobs/Job/*/node-name() , 'omit-xml-declaration' : false() })"/>
                </v2:JobXml>
            </soapenv:Body>
        </soapenv:Envelope>     
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/3NzcBsL Saxon 9.8 HE转换您的输入

<?xml version="1.0" encoding="UTF-8"?><Jobs>
    <Job>
        <JobAction><![CDATA[Add]]></JobAction>
        <ATSJobID><![CDATA[ATSJOBid_SAMPLE123]]></ATSJobID>
        <JobTitle><![CDATA[Senior Launch Commander]]></JobTitle>
        <JobCity><![CDATA[Jacksonville]]></JobCity>
        <JobState><![CDATA[FL]]></JobState>
        <JobCountry><![CDATA[US]]></JobCountry>
        <JobZipCode><![CDATA[32256]]></JobZipCode>
        <JobType><![CDATA[FullTime]]></JobType>
        <JobCode><![CDATA[19]]></JobCode>
    </Job>
</Jobs>

进入结果

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:v2="http://www.smashfly.com/services/simplepost/v2"
                  xmlns:sf="http://schemas.datacontract.org/2004/07/SF.WebHelpers">
   <soapenv:Header/>
   <soapenv:Body>
      <v2:JobXml><![CDATA[<?xml version="1.0" encoding="UTF-8"?><Jobs>
<Job>
<JobAction><![CDATA[Add]]]]><![CDATA[></JobAction>
<ATSJobID><![CDATA[ATSJOBid_SAMPLE123]]]]><![CDATA[></ATSJobID>
<JobTitle><![CDATA[Senior Launch Commander]]]]><![CDATA[></JobTitle>
<JobCity><![CDATA[Jacksonville]]]]><![CDATA[></JobCity>
<JobState><![CDATA[FL]]]]><![CDATA[></JobState>
<JobCountry><![CDATA[US]]]]><![CDATA[></JobCountry>
<JobZipCode><![CDATA[32256]]]]><![CDATA[></JobZipCode>
<JobType><![CDATA[FullTime]]]]><![CDATA[></JobType>
<JobCode><![CDATA[19]]]]><![CDATA[></JobCode>
</Job>
</Jobs>]]></v2:JobXml>
   </soapenv:Body>
</soapenv:Envelope>

如果要使用XSLT 2或1执行此操作,则需要检查是否有可用于序列化节点的扩展功能.

点赞