Expressions allow you to perform simple arithmetic and Boolean logic when processing nodes. Here's an example of some simple addition and multiplication. The document math.xml contains a group of operand elements, each containing an integer: <math> <operand>12</operand> <operand>23</operand> <operand>45</operand> <operand>56</operand> <operand>75</operand> </math> You can use an expression to add and multiply 25 with these operands, as shown in Example 4-10, the stylesheet math.xsl. Example 4-10. A stylesheet that does simple math<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="math"> <xsl:apply-templates select="operand"/> </xsl:template> <xsl:template match="operand"> <xsl:value-of select="."/> <xsl:text> + 25 = </xsl:text> <xsl:value-of select=". + 25"/> <xsl:text> </xsl:text> <xsl:value-of select="."/> <xsl:text> * 25 = </xsl:text> <xsl:value-of select=". * 25"/> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet> The expression is the value of several select attributes of value-of that add and multiply the content of each operand element with 25. The value-of element returns a string value, but the presence of + or * automatically converts the content of operand to a number, if possible. If the content of operand were a nonnumerical string, however, the number conversion wouldn't take place. This won't cause an error, but you will get NaN (Not a Number) in response. When you process math.xsl against math.xml using: xalan math.xml math.xsl you get this result: 12 + 25 = 37 12 * 25 = 300 23 + 25 = 48 23 * 25 = 575 45 + 25 = 70 45 * 25 = 1125 56 + 25 = 81 56 * 25 = 1400 75 + 25 = 100 75 * 25 = 1875 The stylesheet shown in Example 4-11, boolean.xsl, combines addition and multiplication with some Boolean logic. It uses expressions in predicates to test whether the content of operand nodes are both greater-than and less-than a value. Example 4-11. A stylesheet demonstrating more mathematical capability<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:apply-templates select="math"/> </xsl:template> <xsl:template match="math"> <xsl:apply-templates select="operand[(. < 50) and (. > 30)]"/> </xsl:template> <xsl:template match="operand[(. < 50) and (. > 30)]"> <xsl:value-of select="."/> <xsl:text> + 25 = </xsl:text> <xsl:value-of select=". + 25"/> <xsl:text> </xsl:text> <xsl:value-of select="."/> <xsl:text> * 25 = </xsl:text> <xsl:value-of select=". * 25"/> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet> In ordinary English, the expression: (. < 50) and (. > 30) tests whether the operand is less than 50 and greater than 30. The entity references < and > are used in the predicates instead of < and > because < is forbidden in attribute values in XML (see Section 3.1 of the XML specification). To balance this limitation, XML uses entity references for both symbols, even though > is legal in attribute values. The parentheses distinguish the greater-than and less-than tests, which are compared with the and operator. For a complete list of Boolean and math operators in XPath, see Table 4-5.
This concludes your mini math lesson in XPath and XSLT. To learn more about math in XPath, see Sections 3.4 and 3.5 in the XPath specification. |