Multiplying, Dividing, Adding, Subtracting
You can include simple arithmetic operations to your expressions. These will allow you to test for more complicated conditions or to output calculated values (Figure 4.5).
x s l t ... <td valign="top"> <xsl:choose> <xsl:when test=" history/year_destroyed != 0"> <xsl:choose> <xsl:when test=" history/year_destroyed/@era = 'BC'"> <xsl:value-of select=" history/year_built - history/year_destroyed"/> </xsl:when> <xsl:otherwise> <xsl:value-of select=" history/year_built + history/year_destroyed - 1"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:value-of select=" history/year_built + 2008 - 1"/> </xsl:otherwise> </xsl:choose> </td> ...
Figure 4.5. I added a new column to the Overview section called Years Standing. The math is pretty straightforward, but one complicated part of the logic is explained here: Since all the wonders were built in the BC era, if they were destroyed in the AD era or are still standing, I need to add, not subtract, the year_built and the year_destroyed.
Figure 4.6 If you do a little math using this HTML output from the XSLT in Figure 4.6, you’ll see that in 260 years, the Great Pyramid of Giza will have stood longer than all the other wonders combined.
To multiply, divide, add, or subtract:
- Type the first operand. It can be a numerical constant like 12, or it can be a node set (in which case the string value of the first node is used).
- Then, type the mathematical operator: * (for multiplication), div (for division, since / is already fraught with meaning), + (for addition), or – (for subtraction).
- Finally, type the second operand.