2016-02-12 66 views
0

我的要求是爲不包含soap的請求添加soap包裝器,如果請求確實有soap包裝器,則不執行任何操作。添加肥皂信封或使用XSLT 1.0保持相同

案例1:添加簡單XML請求的soap包裝器。

案例2:發送有效負載,以防萬一它已經是SOAP。

代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- This stylesheet adds one or removes a SOAP envelope if one is found --> 
<xsl:stylesheet version="1.0" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.w3.org/1999/xhtml"> 
    <xsl:output method="xml"/> 
    <xsl:template match="/"> 
     <xsl:choose> 
      <xsl:when test="contains(.,'Envelope') = 'true' "> 
       <xsl:copy-of select="/"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
        <soap:Header/> 
        <soap:Body> 
         <xsl:copy-of select="/"/> 
        </soap:Body> 
       </soap:Envelope> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

樣品要求工作之一:

<ProductMovementReport xmlns="urn:cidx:names:specification:ces:schema:all:5:0" Version="5.0"> 
    <Header> 
     <ThisDocumentIdentifier> 
      <DocumentIdentifier>1044911</DocumentIdentifier> 
     </ThisDocumentIdentifier> 
    </Header> 
    <ProductMovementReportBody> 
     <ProductMovementReportDetails> 
      <ReportingEntity> 
       <PartnerInformation> 
        <PartnerName>CHS</PartnerName> 
        <PartnerIdentifier Agency="GLN">123456</PartnerIdentifier> 
       </PartnerInformation> 
      </ReportingEntity> 
     </ProductMovementReportDetails> 
    </ProductMovementReportBody> 
</ProductMovementReport> 

此轉換成SOAP消息成功

Sample_request_WithSOAPwrapper

請求:

<soap:Envelope xmlns:tns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <soap:Header/> 
    <soap:Body> 
     <ProductMovementReport xmlns="urn:cidx:names:specification:ces:schema:all:5:0" Version="5.0"> 
      <Header> 
       <ThisDocumentIdentifier> 
        <DocumentIdentifier>1044911</DocumentIdentifier> 
       </ThisDocumentIdentifier> 
      </Header> 
      <ProductMovementReportBody> 
       <ProductMovementReportDetails> 
        <ReportingEntity> 
         <PartnerInformation> 
          <PartnerName>CHS</PartnerName> 
          <PartnerIdentifier Agency="GLN">123456</PartnerIdentifier> 
         </PartnerInformation> 
        </ReportingEntity> 
       </ProductMovementReportDetails> 
      </ProductMovementReportBody> 
     </ProductMovementReport> 
    </soap:Body> 
</soap:Envelope> 

給出輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:tns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <soap:Header/> 
    <soap:Body> 
     <soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
      <soap:Header/> 
      <soap:Body> 
       <ProductMovementReport xmlns="urn:cidx:names:specification:ces:schema:all:5:0" Version="5.0"> 
        <Header> 
         <ThisDocumentIdentifier> 
          <DocumentIdentifier>1044911</DocumentIdentifier> 
         </ThisDocumentIdentifier> 
        </Header> 
        <ProductMovementReportBody> 
         <ProductMovementReportDetails> 
          <ReportingEntity> 
           <PartnerInformation> 
            <PartnerName>CHS</PartnerName> 
            <PartnerIdentifier Agency="GLN">123456</PartnerIdentifier> 
           </PartnerInformation> 
          </ReportingEntity> 
         </ProductMovementReportDetails> 
        </ProductMovementReportBody> 
       </ProductMovementReport> 
      </soap:Body> 
     </soap:Envelope> 
    </soap:Body> 
</soap:Envelope> 

更新:我沒加什麼,我面臨的問題。

問題是添加了額外的肥皂包裝。

<soap:Envelope xmlns:tns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <soap:Header/> 
    <soap:Body> 
     <soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
      <soap:Header/> 
      <soap:Body> 
       </soap:Body> 
     </soap:Envelope> 
    </soap:Body> 
</soap:Envelope> 

任何人都可以請告知我在哪裏做錯了?

我的猜測是我做錯了測試條件。

回答

1

讓我給出一個不同的方法來解決這個問題:首先,刪除任何現有的soap包裝。然後無條件添加必要的肥皂包裝

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" > 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="/"> 
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
     <soap:Header/> 
     <soap:Body> 
      <xsl:apply-templates/> 
     </soap:Body> 
    </soap:Envelope> 
</xsl:template> 

<xsl:template match="soap:*"> 
    <xsl:apply-templates/> 
</xsl:template> 

</xsl:stylesheet> 
+0

感謝邁克爾指着我的更好的方法。 –