添加肥皂信封或使用 XSLT 1.0 保持不变



我的要求是为不包括肥皂的请求添加肥皂包装纸,如果请求确实有肥皂包装纸,则不要做任何事情。

情况 1:为纯 XML 请求添加肥皂包装器。

情况 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>

谁能告诉我我哪里做错了?

我的猜测是我做错了测试条件。

让我提出一种不同的方法来解决这个问题:首先,移除任何现有的肥皂包装纸。然后无条件添加必要的肥皂包装纸。

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>

相关内容

  • 没有找到相关文章

最新更新