📅  最后修改于: 2020-12-26 06:55:16             🧑  作者: Mango
XPath表达式使用诸如URL之类的路径表示法来寻址XML文档的各个部分。计算该表达式以产生节点集,布尔值,数字或字符串类型的对象。例如,如果book / author表达式在源XML文档中声明,则该表达式将返回
在XPath中,路径表达式用于选择XML文档中的节点或节点集。通过遵循路径或步骤选择节点。
让我们以一个示例来看XPath的语法。在这里,我们获取一个XML文档。
Three Mistakes of My Life
110
Immortals of Meluha
200
用于选择节点的路径表达式为:
Index | Expression | Description |
---|---|---|
1) | nodename | Selects all nodes with the name “nodename” |
2) | / | Selects from the root node. |
3) | // | Selects nodes in the document from the current node that match the selection no matter where they are. |
4) | . | Selects the current node |
5) | .. | Selects the parent of the current node |
6) | @ | Selects attributes |
请参阅以上示例中的路径表达式及其详细信息:
Path Expression | Result |
---|---|
bookstore | Selects all nodes with the name “bookstore” |
/bookstore | Selects the root element bookstore. Note: if the path starts with a slash ( / ) it always represents an absolute path to an element! |
bookstore/book | Selects all book elements that are children of bookstore. |
//book | Selects all book elements no matter where they are in the document. |
bookstore//book | Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element. |
//@lang | Selects all attributes that are named lang. |
谓词用于查找特定节点或包含特定值的节点。
谓词始终嵌入方括号中。
在下表中,我们列出了一些带有谓词的路径表达式以及表达式的结果:
Path Expression | Result |
---|---|
/bookstore/book[1] | Selects the first book element that is the child of the bookstore element. Note: In IE 5,6,7,8,9 first node is[0], but according to W3C, it is [1]. To solve this problem in IE, set the selectionlanguage to XPath:
in JavaScript: xml.setProperty(“SelectionLanguage”,”XPath”);
|
/bookstore/book[last()] | Selects the last book element that is the child of the bookstore element. |
/bookstore/book[last()-1] | Selects the last but one book element that is the child of the bookstore element. |
/bookstore/book[position()<3] | Selects the first two book elements that are children of the bookstore element. |
//title[@lang] | Selects all the title elements that have an attribute named lang. |
//title[@lang=’en’] | Selects all the title elements that have a “lang” attribute with a value of “en”. |
/bookstore/book[price>100] | Selects all the book elements of the bookstore element that have a price element with a value greater than 100 |
/bookstore/book[price>100]/title | Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 100 |
XPath通配符用于选择未知的XML节点。
Wildcard | Description |
---|---|
* | Matches any element node |
@* | Matches any attribute node |
node() | Matches any node of any kind |
参见上面的示例:
Path Expression | Result |
---|---|
/bookstore/* | Selects all the child element nodes of the bookstore element |
//* | Selects all elements in the document |
//title[@*] | Selects all title elements which have at least one attribute of any kind |
| XPath表达式中使用运算符选择多个路径。从上面的示例中,我们列出了一些路径表达式和表达式的结果。
Path Expression | Result |
---|---|
//book/title | //book/price | Selects all the title and price elements of all book elements |
//title | //price | Selects all the title and price elements in the document |
/bookstore/book/title | //price | Selects all the title elements of the book element of the bookstore element and all the price elements in the document |