XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Numbering, counting and summing / Counting, summing and rounding

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)

XPath functions can count nodes and sum numeric contents using fn:count() and fn:sum() respectively. XPath can round the resulting sum, using the familiar functions fn:round(), fn:floor() and fn:ceiling() or the more unusual fn:round-half-to-even(). The book list demonstrates them all.

Counting with XPath – fn:count()

Some books have an ISBN and others do not. To count each group, use function fn:count(), which constructs a sequence and counts its items. First count all books, preferably in the template rule for <buchhandel>:

<xsl:template match="buchhandel">
  <p><b>Statistik:</b></p>
  <p>Bücher in der Liste: <xsl:value-of select="fn:count(buch)"/></p>
  <xsl:apply-templates select="buch"/>
</xsl:template>

Starting with <buchhandel> as current node, fn:count(buch) constructs the sequence of book elements and returns its size. Predicates make this more precise:

<xsl:template match="buchhandel">
  <p><b>Statistik:</b></p>
  <p>Bücher in der Liste: <xsl:value-of select="fn:count(buch)"/>; davon mit ISBN: <xsl:value-of select="fn:count(buch[@isbn!=''])"/>; ohne ISBN: <xsl:value-of select="fn:count(buch[fn:not(@isbn!='')])"/>;</p>
  <xsl:apply-templates select="buch"/>
</xsl:template>

Code example: kap02/2.03.3/buchhandel-count-sum.xsl (excerpt).

The output is:

Bücher in der Liste: 27; davon mit ISBN: 12; ohne ISBN: 15;

Summing with XPath – fn:sum()

To obtain the total price of all books, however useful that may be, use function fn:sum(). Extend the statistics above:

...
<p>Gesamtpreis aller Bücher: <xsl:value-of select="fn:sum(buch/preis)"/></p>
...

Code example: kap02/2.03.3/buchhandel-count-sum.xsl (excerpt).

The output price is 612.7, an unconventional price format that we will fix. First calculate the average. The book count was already obtained with fn:count(); here it is used again:

<p>Durchschnittspreis aller Bücher: <xsl:value-of select="fn:sum(buch/preis) div fn:count(buch)"/></p>

Code example: kap02/2.03.3/buchhandel-count-sum.xsl (excerpt).

Note operator div. The symbol / used for division elsewhere has another XPath meaning. The other familiar arithmetic operators are +, – , *. There are also mod for modulo and idiv for integer division. One might try the latter here (Note: Using idiv causes an error because its first operand must strictly be an integer, unlike here). The actual result is 22.6925925925925925926.

Fortunately, XPath also rounds numbers.

Rounding with XPath – fn:round(), fn:floor(), fn:ceiling(), fn:round-half-to-even()

First try fn:round(). This is easy, but the expression is getting nested:

<p>Durchschnittspreis aller Bücher: <xsl:value-of select="fn:round(fn:sum(buch/preis) div fn:count(buch))"/></p>

Code example: kap02/2.03.3/buchhandel-count-sum-round.xsl (excerpt).

This rounds up to 23, like function fn:cei­ling(), whereas function fn:floor() rounds down to 22. For prices, two decimal places would be better.

Use fn:round-half-to-even() for that. Its second argument sets precision: 2 produces exactly two decimal places:

fn:round-half-to-even(fn:sum(buch/preis) div fn:count(buch),2)

Code example: kap02/2.03.3/buchhandel-count-sum-roundeven.xsl (excerpt).

The output is:

Durchschnittspreis aller Bücher: 22.69

Formatting numbers with format-number()

A final cosmetic improvement gives the total two decimal places. Using fn:round-half-to-even() again is ineffective: it removes excess decimal places but cannot append trailing zeros.

Instead use XSLT function format-number(). Besides the number, it takes a pattern string. (Note: The function is much more complex; see the XSLT and XPath function reference.) The pattern uses placeholders: # marks an optional digit, 0 a digit always output, filled with 0 when needed.

This call achieves the result:

format-number(fn:sum(buch/preis),'##00.00')

Code example: kap02/2.03.3/buchhandel-count-sum-format.xsl (excerpt).

The output is now:

Gesamtpreis aller Bücher: 612.70

To ensure that the value rounded above using fn:round-half-to-even() always displays two decimal places, even for a simpler average, also apply format-number() here:

format-number(fn:round-half-to-even(fn:sum(buch/preis) div fn:count(buch), 2),'##00.00')

Code example: kap02/2.03.3/buchhandel-count-sum-format.xsl (excerpt).

The appearance is unchanged here; this is just a safeguard. That concludes the book statistics for now. Next consider output order.

   

back to top