Boolean "sum" in XSLT

I know I can sum multiple nodes with numerical values. How can I perform a "boolean sum" over a set of nodes? For example:

<a>
  <b>false</b>
  <b>false</b>
  <b>true</b>
  <b>false</b>
</a>

      

How can I get the logical OR of all <b>

node values ? (which must be "true").

+3


source to share


2 answers


Using

boolean(/*/b[. = 'true'])

      

This creates the boolean value of the expression :

 /*/b[. = 'true']

      



and is true when the specified expression selects at least one node , that is, when exists b

, which is a child of the top element and whose string value is a string 'true

.


If you also want to compute the "logical product" (using and), do:

not(/*/b[. = 'false'])

      

+3


source


You can calculate the true values:

<xsl:if test="count(a/b[text()='true']) > 0">
        true
</xsl:if>

      



If there are any true values, OR will be true.

0


source







All Articles