XPath 是 XSLT 标准中的一个主要元素。
XPath 可用于浏览 XML 文档中的元素和属性。
|
XPath 使用路径表达式来选择 XML 文档中的节点或节点集。这些路径表达式看起来非常类似于您在使用传统计算机文件系统时看到的表达式。
XPath 表达式可用于 JavaScript、Java、XML Schema、PHP、Python、C 和 C++ 以及许多其他语言。
XPath 是 XSLT 标准中的一个主要元素。
有了 XPath 知识,您将能够充分利用 XSL。
我们将使用以下 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
在下表中,我们列出了一些 XPath 表达式以及表达式的结果:
XPath Expression | Result |
---|---|
/bookstore/book[1] | Selects the first book element that is the child of the bookstore element |
/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>35.00] | Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 |
/bookstore/book[price>35.00]/title | Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00 |
您将在我们的文章中了解有关 XPath 的更多信息XPath 教程。
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!