Kumar_y
Kumar_y

Reputation: 239

Adding soap envelope or remain same using XSLT 1.0

My requirement is to add the soap wrapper for the request which is doesn't include soap and If the request does have the soap wrapper don't do anything.

Case 1: ADD the soap wrapper for plain XML request.

Case 2: Send the payload as it in case its already SOAP.

Code:

<?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>

Sample request working one:

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

This converts into SOAP message successfully

Sample_request_WithSOAPwrapper

Request:

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

Gives output:

<?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>

Update: I didn't add what's the problem I am facing.

The problem is extra soap wrapper is being added.

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

Can anyone please advise where I am doing it wrong?

My guess is I am doing the test condition wrong.

Upvotes: 0

Views: 387

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116992

Let me suggest a different approach to the problem: First, remove any existing soap wrappers. Then add the necessary soap wrappers unconditionally.

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>

Upvotes: 1

Related Questions