XML and XPath
Wat is XPath?
XPath is een belangrijk element in de XSLT-standaard.
XPath kan worden gebruikt om elementen en eigenschappen in een XML-document te lokaliseren (navigeren)
XPath

- XPath is een syntaxis voor het definiëren van verschillende delen van een XML-document
- XPath gebruikt padexpressies om in een XML-document te navigeren
- XPath bevat een standaard functiebibliotheek
- XPath is een belangrijk element in XSLT en XQuery
- XPath is een aanbevolen standaard van de W3C
XPath padexpressie
XPath gebruikt padexpressies om knopen of knopenverzamelingen in een XML-document te selecteren. Deze padexpressies lijken zeer sterk op de expressies die u ziet wanneer u een traditionele computerbestandssysteem gebruikt.
XPath-expressies kunnen worden gebruikt in talen zoals JavaScript, Java, XML Schema, PHP, Python, C en C++ en vele andere.
XPath wordt gebruikt in XSLT
XPath is een belangrijk element in de XSLT-standaard.
Wanneer u XPath begrijpt, kunt u XSL optimaal gebruiken.
XPath Examples
Wij zullen de volgende XML-document gebruiken:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="美食"> <title lang="zh">雅舍谈吃</title> <author>梁实秋</author> <year>2013</year> <price>35</price> </book> <book category="儿童"> <title lang="zh">了不起的狐狸爸爸</title> <author>罗尔德·达尔</author> <year>2009</year> <price>10.00</price> </book> <book category="文学"> <title lang="zh">将熟悉变为陌生</title> <author>齐格蒙·鲍曼</author> <author>彼得·哈夫纳</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="政治"> <title lang="zh">论美国的民主</title> <author>托克维尔</author> <year>1989</year> <price>60.00</price> </book> </bookstore>
In the table below, we list some XPath expressions and their results:
XPath Expression | Result |
---|---|
/bookstore/book[1] | Select the first book element that is a child element of the bookstore element. |
/bookstore/book[last()] | Select the last book element that is a child element of the bookstore element. |
/bookstore/book[last()-1] | Select the second-to-last book element that is a child element of the bookstore element. |
/bookstore/book[position()<3] | Select the first two book elements that are child elements of the bookstore element. |
//title[@lang] | Select all title elements that have an attribute named 'lang'. |
//title[@lang='en'] | Select all title elements that have an attribute 'lang' with the value 'en'. |
/bookstore/book[price>35.00] | Select all book elements within the bookstore element where the value of the price element is greater than 35.00. |
/bookstore/book[price>35.00]/title | Selecteer alle title elementen van het book element binnen bookstore, waarbij de waarde van het price element groter is dan 35.00. |
XPath Handleiding
Je zult in onze XPath Handleiding leren over XPath Meer kennis.