- XPath 教程
- XPath - 主頁
- XPath - 概述
- XPath - 表示式
- XPath - 節點
- XPath - 絕對路徑
- XPath - 相對路徑
- XPath - 軸
- XPath - 運算子
- XPath - 萬用字元
- XPath - 謂詞
- XPath 有用資源
- XPath - 快速指南
- XPath - 有用資源
- XPath - 討論
XPath - 表示式
XPath 表示式通常定義一個模式來選擇一組節點。這些模式由 XSLT 使用來執行轉換,或由 XPointer 使用來定位。
XPath 規範指定可作為 XPath 表示式執行結果的七種型別的節點。
- 根
- 元素
- 文字
- 屬性
- 註釋
- 處理指令
- 名稱空間
XPath 使用路徑表示式從 XML 文件中選擇節點或節點列表。
以下是用於從 XML 文件中選擇任何節點/節點列表的有用路徑和表示式的列表。
| 序列號。 | 表示式和描述 |
|---|---|
| 1 | node 名字 選擇給定名稱“node 名字”的所有節點 |
| 2 | / 選擇從根節點開始 |
| 3 | // 選擇從與選擇匹配的當前節點開始 |
| 4 | . 選擇當前節點 |
| 5 | .. 選擇當前節點的父節點 |
| 6 | @ 選擇屬性 |
| 7 | student 示例 - 選擇名稱為“student”的所有節點 |
| 8 | class/student 示例 - 選擇所有 class 的子級的 student 元素 |
| 9 | //student 無論它們在文件中的什麼位置,選擇所有 student 元素 |
示例
在此示例中,我們建立了一個示例 XML 文件 students.xml 及其樣式表文檔 **students.xsl**,它使用各種 XSL 標記的 **select** 屬性下的 XPath 表示式來獲取每個 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">
<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>
驗證輸出
廣告