首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xsl将SOAP转换为XML

使用xsl将SOAP转换为XML
EN

Stack Overflow用户
提问于 2022-02-01 17:10:57
回答 2查看 60关注 0票数 0

我正试图使用XSL将SOAP转换为XML,但是我已经研究了太久,无法确定我是如何出错的。有什么想法吗?

SOAP

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="https://XXX.disclosures.co.uk/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:InitiateApplicationResponse>
         <InitiateApplicationResponse>
            <Application>
               <ApplicantReferenceNumber>580064065</ApplicantReferenceNumber>
               <Forename>SEASHELLS</Forename>
               <Surname>OSEAS</Surname>
               <InitiateStatus>SUCCESS</InitiateStatus>
               <Error />
               <StatusCode>202</StatusCode>
               <StatusDescription>e-Invitation Sent</StatusDescription>
               <TransactionReferenceNumber>202201310003</TransactionReferenceNumber>
            </Application>
         </InitiateApplicationResponse>
      </ns1:InitiateApplicationResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

XSL

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0">
   <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="https://XXX.disclosures.co.uk/"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
         <xsl:template match="ns1:InitiateApplicationResponse">
            <xsl:for-each select="Application">
               <EXCHANGE>
                  <SPD>
                     <SPD.SRS>
                        <SPD_STUC.SPD.SRS>
                           <xsl:value-of select="ApplicantReferenceNumber" />
                        </SPD_STUC.SPD.SRS>
                        <SPD_SEQN.SPD.SRS>CRB</SPD_SEQN.SPD.SRS>
                        <SPD_UDF1.SPD.SRS>
                           <xsl:value-of select="TransactionReferenceNumber" />
                        </SPD_UDF1.SPD.SRS>
                     </SPD.SRS>
                  </SPD>
               </EXCHANGE>
            </xsl:for-each>
         </xsl:template>
      </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>
</xsl:stylesheet>

试图使用上面的SOAP和XSL转换成这个XML。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<EXCHANGE>
   <SPD>
      <SPD.SRS>
         <SPD_STUC.SPD.SRS>580064065</SPD_STUC.SPD.SRS>
         <SPD_SEQN.SPD.SRS>CRB</SPD_SEQN.SPD.SRS>
         <SPD_UDF1.SPD.SRS>202201310003</SPD_UDF1.SPD.SRS>
      </SPD.SRS>
   </SPD>
</EXCHANGE>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-01 19:47:11

请尝试以下XSLT。

XSLT

代码语言:javascript
复制
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml" encoding="utf-8"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="Application">
        <EXCHANGE>
            <SPD>
                <SPD.SRS>
                    <SPD_STUC.SPD.SRS>
                        <xsl:value-of select="ApplicantReferenceNumber"/>
                    </SPD_STUC.SPD.SRS>
                    <SPD_SEQN.SPD.SRS>CRB</SPD_SEQN.SPD.SRS>
                    <SPD_UDF1.SPD.SRS>
                        <xsl:value-of select="TransactionReferenceNumber"/>
                    </SPD_UDF1.SPD.SRS>
                </SPD.SRS>
            </SPD>
        </EXCHANGE>
    </xsl:template>
</xsl:stylesheet>

输出XML

代码语言:javascript
复制
<?xml version='1.0' encoding='utf-8' ?>
<EXCHANGE>
  <SPD>
    <SPD.SRS>
      <SPD_STUC.SPD.SRS>580064065</SPD_STUC.SPD.SRS>
      <SPD_SEQN.SPD.SRS>CRB</SPD_SEQN.SPD.SRS>
      <SPD_UDF1.SPD.SRS>202201310003</SPD_UDF1.SPD.SRS>
    </SPD.SRS>
  </SPD>
</EXCHANGE>
票数 1
EN

Stack Overflow用户

发布于 2022-02-02 10:47:47

如果您的消息中有更多内容,那么只需要SOAP:Body,并且想知道如何使用xsl处理名称空间,那么您可能需要这样做:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:ns1="https://XXX.disclosures.co.uk/"
  exclude-result-prefixes="SOAP-ENV ns1"
  version="1.0" 
  >
  <xsl:output indent="yes" method="xml" encoding="utf-8"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:apply-templates select="SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:InitiateApplicationResponse/InitiateApplicationResponse/Application"/>
  </xsl:template>
  
  <xsl:template match="Application">
      <EXCHANGE>
        <SPD>
          <SPD.SRS>
            <SPD_STUC.SPD.SRS>
              <xsl:value-of select="ApplicantReferenceNumber" />
            </SPD_STUC.SPD.SRS>
            <SPD_SEQN.SPD.SRS>CRB</SPD_SEQN.SPD.SRS>
            <SPD_UDF1.SPD.SRS>
              <xsl:value-of select="TransactionReferenceNumber" />
            </SPD_UDF1.SPD.SRS>
          </SPD.SRS>
        </SPD>
      </EXCHANGE>
  </xsl:template>
</xsl:stylesheet>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70944689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档