使用XSLT 1.0添加soap信封或保持相同

时间:2016-02-12 17:28:57

标签: xslt xslt-1.0

我的要求是为不包含soap的请求添加soap包装器。如果请求确实让soap包装器没有做任何事情。

案例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 个答案:

答案 0 :(得分:1)

让我建议一个解决问题的方法:首先,删除任何现有的肥皂包装。然后无条件地添加必要的肥皂包装

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>