Publishers of technology books, eBooks, and videos for creative people

XPath Functions

• Print
This chapter is from the book

This chapter is from the book 

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.

To multiply, divide, add, or subtract:

1. 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).
2. Then, type the mathematical operator: * (for multiplication), div (for division, since / is already fraught with meaning), + (for addition), or (for subtraction).
3. Finally, type the second operand.