与XPath表达式一起使用的数字运算符的列表:
Index | Operator | Description |
---|---|---|
1) | + | 用于加法运算。 |
2) | – | 用于减法运算。 |
3) | * | 用于乘法运算。 |
4) | div | 用于除法运算。 |
5) | mod | 用于模运算 |
XPath表达式使用的数字功能列表:
Index | Function | Description |
---|---|---|
1) | ceiling() | 它用于返回大于所提供值的最小整数。 |
2) | floor() | 它用于返回小于提供的值的最大整数。 |
3) | round() | 用于将舍入后的值返回到最接近的整数。 |
4) | sum() | 它用于返回两个数字的和。 |
XPath编号运算符/函数示例
让我们以创建一个<employee>元素表为例, 该表具有其属性ID和其子元素<firstname>, <lastname>, <nickname>和<salary>。它计算员工的薪水, 然后显示结果。
employee.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
<employee id = "001">
<firstname>Abhiram</firstname>
<lastname>Kushwaha</lastname>
<nickname>Manoj</nickname>
<salary>15000</salary>
</employee>
<employee id = "002">
<firstname>Akash</firstname>
<lastname>Singh</lastname>
<nickname>Bunty</nickname>
<salary>25000</salary>
</employee>
<employee id = "003">
<firstname>Brijesh</firstname>
<lastname>Kaushik</lastname>
<nickname>Ballu</nickname>
<salary>20000</salary>
</employee>
<employee id = "004">
<firstname>Zoya</firstname>
<lastname>Mansoori</lastname>
<nickname>Sonam</nickname>
<salary>30000</salary>
</employee>
</class>
员工.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Employee</h2>
<table border = "1">
<tr bgcolor = "pink">
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Salary</th>
<th>Grade</th>
</tr>
<xsl:for-each select = "class/employee">
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "salary"/></td>
<td>
<xsl:choose>
<xsl:when test = "salary div 25000 > 1">
High
</xsl:when>
<xsl:when test = "salary div 20000 > 1">
Medium
</xsl:when>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出
评论前必须登录!
注册