I have a relatively simple Soap response message XML which I’d like to modify with XSLT.
Here’s what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:z="http://some.url/WS/">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="Method" select="name(//soap:Body/*)" />
<xsl:template match="//soap:Body/*" >
<xsl:choose>
<xsl:when test="$Method='Method1Response'">
<xsl:call-template name="Method1" />
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="Method1" match="//soap:Body/z:Method1Response/z:Method1Result/z:Errors" />
</xsl:stylesheet>
Sample XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<env:Header xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsa:Action></wsa:Action>
<wsa:MessageID></wsa:MessageID>
<wsa:RelatesTo></wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
</env:Header>
<soap:Body>
<Method1Response xmlns="http://some.url/WS/">
<Method1Result>
<Msg>blah blah</Msg>
<Errors>
<ErrorItem>error 1</ErrorItem>
<ErrorItem>error 2</ErrorItem>
</Errors>
<Data />
</Method1Result>
</Method1Response>
</soap:Body>
</soap:Envelope>
The idea is to delete the Errors from under Method1Result, if Method1 = specific value. If not, leave it as is. What I currently have, itsn’t doing it. Thanks.
Another edit to further clarify:
I want to have a single XSLT file for multiple XML files which belong to different web service calls. Thise means that Method1 can have a number of different values for example: GetMeal, GetWindow, GetHandle, GetWeatherReport,… I’m hoping to create a “case” for each value within the same XSLT in order to intentionally ‘break’ the expected XML for testing purposes. Each time I will delete a different element.
The problem is the template that matches
//soap:Body/*calls a named template which itself does not output anything. This results in all elemenets under soap:Body being ignored.As you already have a template to match the elements you want to remove, you can remove the template that matches soap:Body and you should find it will work as expected.
when this applied to your sample XML, the following is output
EDIT: There is no reason why you can’t have further matching templates to handle other method responses you might get back. For example….