XML và XPath
XPath là gì?
XPath là một yếu tố chính trong tiêu chuẩn XSLT.
XPath có thể được sử dụng để điều hướng qua các phần tử và thuộc tính trong tài liệu XML.
|
Biểu thức đường dẫn XPath
XPath sử dụng các biểu thức đường dẫn để chọn các nút hoặc tập hợp nút trong tài liệu XML. Các biểu thức đường dẫn này trông rất giống với các biểu thức bạn thấy khi làm việc với hệ thống tệp máy tính truyền thống.
Biểu thức XPath có thể được sử dụng trong JavaScript, Java, Lược đồ XML, PHP, Python, C và C ++, và rất nhiều ngôn ngữ khác.
XPath được sử dụng trong XSLT
XPath là một yếu tố chính trong tiêu chuẩn XSLT.
Với kiến thức XPath, bạn sẽ có thể tận dụng tối đa XSL.
Ví dụ XPath
Chúng tôi sẽ sử dụng tài liệu XML sau:
<?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>
Trong bảng dưới đây, chúng tôi đã liệt kê một số biểu thức XPath và kết quả của các biểu thức:
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 |
Hướng dẫn XPath
Bạn sẽ tìm hiểu thêm nhiều điều về XPath trong Hướng dẫn XPath của chúng tôi .