- XPath 教程
- XPath - 主页
- XPath - 概述
- XPath - 表达式
- XPath - 节点
- XPath - 绝对路径
- XPath - 相对路径
- XPath - 轴
- XPath - 运算符
- XPath - 通配符
- XPath - 谓词
- XPath 有用资源
- XPath - 快速指南
- XPath - 有用资源
- XPath - 讨论
XPath - 谓词
谓词是指用方括号编写的 XPath 表达式。它是指在某个条件下限制节点集中选定的节点。例如,
序号 | 谓词和说明 |
---|---|
1 | /class/student[1] 选择第一个作为 class 元素子级的 student 元素。 |
2 | /class/student[last()] 选择最后一个作为 class 元素子级的 student 元素。 |
3 | /class/student[@rolllno = 493] 选择学生学号为 493 的 student 元素。 |
4 | /class/student[marks>85] 选择 marks > 85 的 student 元素。 |
示例
通过对每个学生进行迭代,此示例创建了一个包含所有具有其详细信息的 <student> 元素的表。它计算学生节点的位置,然后打印出学生详细信息以及序列号。
students.xml
<?xml version = "1.0"?> <?xml-stylesheet type = "text/xsl" href = "students.xsl"?> <class> <student rollno = "393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno = "593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class>
students.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>Students</h2> <table border = "1"> <tr bgcolor = "#9acd32"> <th>Roll No</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Marks</th> </tr> <xsl:for-each select = "/class/student[1]"> <tr> <td><xsl:value-of select = "@rollno"/></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 = "marks"/></td> </tr> </xsl:for-each> <xsl:for-each select = "/class/student[last()]"> <tr> <td><xsl:value-of select = "@rollno"/></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 = "marks"/></td> </tr> </xsl:for-each> <xsl:for-each select = "/class/student[@rollno = 493]"> <tr> <td><xsl:value-of select = "@rollno"/></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 = "marks"/></td> </tr> </xsl:for-each> <xsl:for-each select = "/class/student[marks > 85]"> <tr> <td><xsl:value-of select = "@rollno"/></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 = "marks"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
验证输出结果
广告