XML e XPath
O que é XPath?
XPath é um elemento importante no padrão XSLT.
XPath pode ser usado para navegar pelos elementos e atributos em um documento XML.
|
Expressões de caminho XPath
XPath usa expressões de caminho para selecionar nós ou conjuntos de nós em um documento XML. Essas expressões de caminho se parecem muito com as expressões que você vê quando trabalha com um sistema de arquivos de computador tradicional.
Expressões XPath podem ser usadas em JavaScript, Java, XML Schema, PHP, Python, C e C++ e muitas outras linguagens.
XPath é usado em XSLT
XPath é um elemento importante no padrão XSLT.
Com o conhecimento XPath você poderá tirar grande proveito do XSL.
Exemplo de XPath
Usaremos o seguinte documento 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>
Na tabela abaixo listamos algumas expressões XPath e o resultado das expressões:
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 |
Tutorial XPath
Você aprenderá muito mais sobre XPath em nosso Tutorial XPath .