Is it possible to count the instances of a for-each boolean statement. for example, say you have patients in a hospital and certain criteria must be met to discharge them. 100(%0) health would equal a discharge, pseudo code would read something like this:

totalHealth - HighTemperature + medication * recoveryPeriod

could you then have a choose statement to count the number of patients discharged or still in hospital? eg

<xsl:choose>
<xsl:when test="(totalHealth - HighTemperature + medication * recoveryPeriod) &gt; = 90">
Discharged Patients - <xsl:value-of select="count(true)"/>
</xsl:when>
<xsl:otherwise>
Current Patients - <xsl:value-of select="count(false)"/>
</xsl:otherwise>
<xsl:choose>

I know this code is incorrect, I just want to know if it is possible.

Thanks in advance

Dani AI

Generated

Yes — count the patient nodes that satisfy the discharge test rather than trying to count raw boolean values. The idiomatic XPath/XSLT approach is to select the patient nodes for which the condition is true and apply count() to that node-set; the number of current patients can then be derived from the document total minus that count. Passing a boolean directly into count() (for example count(true)) is not the right tool: count() expects a node-set, not a raw boolean.

About storing the condition in a variable: in XSLT 1.0 variables hold values computed at bind time (node-sets, strings, numbers, booleans) and cannot be used to store an XPath expression for later evaluation in a portable, standard way. Common patterns are to compute the boolean inside the per-patient context (or store the boolean result into a variable for that node) or to factor the test into a named template/stylesheet routine. Some processors provide nonstandard evaluate extensions; XSLT 2.0/3.0 offer stronger alternatives (user functions, and XSLT 3.0’s dynamic evaluation facilities) for more dynamic use-cases.

For reuse and clarity encapsulate the discharge logic in a single place (a named template or user-defined function) and call that from predicates used in counts. In XSLT 2.0 it’s also common to convert boolean results to 1/0 and sum() them when weighted counts or more complex aggregation is needed.

Practical tips: coerce element text to numbers before arithmetic (to avoid whitespace/string surprises), provide defaults for missing nodes, and be careful with operator precedence in compound expressions. For moderate-to-large XML sets the count(predicate) pattern is simpler and usually faster than manual recursion; if performance or streaming is a concern, consider keys, streaming, or processor-specific optimizations. This complements ’s pointer to XPath boolean handling and ’s recursion thought — recursion is possible, but not necessary for the basic counting task.

Recommended Answers

All 9 Replies

show me the xml data
where is tag true

think it is possible

I actually don't have any code to go with this question. It is just something I was wondering if you could do. I know you can count nodes, but I was just curious if you could count the number of times a boolean condition returned true or false.

look here

Thank you, I have looked through this already and could not find the answer, although it won't hurt to view it again. I was wondering if you could do something like this
Discharged: <xsl:value-of select="count(patients[boolean_exp = 'true'])"/> and because my brain is now on a tangent, can you wrap the boolean exression in a variable? So Discharged: <xsl:value-of select="count(patients[$boolean_exp = 'true'])"/>

unter xpath function
bool operation

Helmut Hagemann

Thanks Helmut, so much reading to do, so little time. It looks like I would be better off using a recursive template to count the instances. At least I now know it can be done. Thanks

can you show me the xml file
then I can help you, the nodes to count

can you show me the xml file
then I can help you, the nodes to count

Hi Helmut. I don't have any code to accompany this, it was just a generic question to see if it was possible to count instances of boolean returns. The links have have posted indicate it is, so thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.