XPath also includes a range of operators you can take advantage of when using number values within your expressions.
Number OperatorsHere's a list of number operators you can use in your XPath expressions:
| Operator | Description |
|---|---|
| + | Used for addition. |
| - | Used for subtraction. |
| * | Used for multiplication. |
| div | Used for division. |
| mod | Returns the modulus of two numbers. (The modulus is the remainder after you divide the two numbers). |
Here's a list of functions you can use with numbers in your XPath expressions:
| Function | Description |
|---|---|
| ceiling() | Returns the smallest integer that is larger than the value provided. |
| floor() | Returns the largest integer that is smaller than the value provided. |
| round() | Rounds the value provided to the nearest integer. |
| sum() | Returns the sum of the two numbers provided. |
The Source XML File
We'll use the following XML file, which contains a list of vegetables and their associated nutritional value:
<?xml version="1.0"?> <food_list> <food_item type="vegetable"> <name>Agar</name> <carbs_per_serving>81</carbs_per_serving> <fiber_per_serving>8</fiber_per_serving> <fat_per_serving>0.5</fat_per_serving> <kj_per_serving>1280</kj_per_serving> </food_item> <food_item type="vegetable"> <name>Asparagus</name> <carbs_per_serving>1</carbs_per_serving> <fiber_per_serving>1</fiber_per_serving> <fat_per_serving>0</fat_per_serving> <kj_per_serving>40</kj_per_serving> </food_item> <food_item type="vegetable"> <name>Cabbage</name> <carbs_per_serving>0</carbs_per_serving> <fiber_per_serving>1</fiber_per_serving> <fat_per_serving>0</fat_per_serving> <kj_per_serving>14</kj_per_serving> </food_item> <food_item type="vegetable"> <name>Potato</name> <carbs_per_serving>21.5</carbs_per_serving> <fiber_per_serving>2</fiber_per_serving> <fat_per_serving>1</fat_per_serving> <kj_per_serving>460</kj_per_serving> </food_item> <food_item type="vegetable"> <name>Pumpkin</name> <carbs_per_serving>6</carbs_per_serving> <fiber_per_serving>1</fiber_per_serving> <fat_per_serving>0.5</fat_per_serving> <kj_per_serving>150</kj_per_serving> </food_item> <food_item type="vegetable"> <name>Yam</name> <carbs_per_serving>30.5</carbs_per_serving> <fiber_per_serving>2</fiber_per_serving> <fat_per_serving>0.5</fat_per_serving> <kj_per_serving>550</kj_per_serving> </food_item> <food_item type="vegetable"> <name>Zucchini</name> <carbs_per_serving>1.5</carbs_per_serving> <fiber_per_serving>1.5</fiber_per_serving> <fat_per_serving>0.5</fat_per_serving> <kj_per_serving>55</kj_per_serving> </food_item> </food_list>
The Requirement
This time, for each record, we'll display the carbohydrate value ("carbs"). We will also display a column that multiplies the number of carbs by 3 (representing 3 meals per day). Like this:
The Solution
We could modify the code from the previous lesson so that only the top 5 records are selected. We could achieve this using the XPath <= operator (along with the position() function), as follows:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="food_list"> <table border="1" style="background-color:#cccc00"> <tr> <th>Food Item</th> <th>Carbs Per Serving</th> <th>Carbs Per Day (3 serves per day)</th> </tr> <xsl:for-each select="food_item"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="carbs_per_serving"/></td> <td><xsl:value-of select="carbs_per_serving * 3"/></td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>
No comments:
Post a Comment