I have a xml:
0
I want to implement logic that will return true if one of the success tags is equal to 0 and false if all of them are equal to 0.
First, let's make the truth table:
All zeros | None zero | Some zero, others not
-------------------------------------------
False | False | True
Second, node-set comparison in XPath are existencial. So:
boolean(/soapenv:Envelope/soapenv:Body[success = 0 and success != 0])
It will return true
or false
boolean value.
This input
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<success>0</success>
<success>0</success>
<success>1</success>
</soapenv:Body>
</soapenv:Envelope>
With this stylesheet
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
<xsl:template match="/">
<xsl:value-of
select="boolean(
/soapenv:Envelope
/soapenv:Body[
success = 0 and success != 0
]
)"/>
</xsl:template>
</xsl:stylesheet>
Returns
true
Check it in http://www.utilities-online.info/xsltransformation/?save=ceed7a50-6a47-4c2c-b344-86ad3b3b0d92-xsltransformation
How about:
<xsl:template match="/">
<errorFlag>
<xsl:value-of select="not(/soapenv:Envelope/soapenv:Body/success=1)" />
</errorFlag>
</xsl:template>
Or (requires XSLT 2.0):
<xsl:template match="/">
<errorFlag>
<xsl:value-of select="every $item in /soapenv:Envelope/soapenv:Body/success satisfies $item=0"/>
</errorFlag>
</xsl:template>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="succKey" match="success" use="." />
<xsl:template match="/">
<xsl:call-template name="test" />
</xsl:template>
<xsl:template match="/soapenv:Envelope/soapenv:Body" name="test">
<xsl:variable name="key-count"
select="count(//success[generate-id() =
generate-id(key('succKey', .))])" />
<errorFlag>
<xsl:choose>
<xsl:when test="$key-count = 1">
<xsl:text>true</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>false</xsl:text>
</xsl:otherwise>
</xsl:choose>
</errorFlag>
</xsl:template>
</xsl:stylesheet>
http://xsltfiddle.liberty-development.net/6r5Gh33