Sie sind auf Seite 1von 14

(XPath)

Debajyoti Karmaker American International University-Bangladesh d.karmaker@aiub.edu

Intro

XPath is used to navigate through elements and attributes in an XML document. XPath is a major element in W3C's XSLT standard - and XQuery and XPointer are both built on XPath expressions.

Intro
XPath

is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT XPath is a W3C recommendation

XPath Terminology
Seven

kinds of nodes:

element attribute

text Namespace processing-instruction Comment document nodes

Relationship of Nodes
Parent Children Siblings

Ancestors
Descendants

Selecting Nodes
Expression nodename / Description Selects all child nodes of the named node Selects from the root node

//
. .. @

Selects nodes in the document from the current node that match the selection no matter where they are
Selects the current node Selects the parent of the current node Selects attributes

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>

Path Expression bookstore /bookstore

Result Selects all the child nodes of the bookstore element Selects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! Selects all book elements that are children of bookstore Selects all book elements no matter where they are in the document Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element Selects all attributes that are named lang

bookstore/book //book bookstore//book

//@lang

Predicates
Path Expression /bookstore/book[1] /bookstore/book[last()] /bookstore/book[last()-1] /bookstore/book[position()<3] Result Selects the first book element that is the child of the bookstore element. Selects the last book element that is the child of the bookstore element Selects the last but one book element that is the child of the bookstore element Selects the first two book elements that are children of the bookstore element

//title[@lang]
//title[@lang='eng'] /bookstore/book[price>35.00]

Selects all the title elements that have an attribute named lang
Selects all the title elements that have an attribute named lang with a value of 'eng' 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

Selecting Unknown Nodes


Wildcard * @* node() Description Matches any element node Matches any attribute node Matches any node of any kind Result Selects all the child nodes of the bookstore element Selects all elements in the document Selects all title elements which have any attribute

Path Expression /bookstore/* //* //title[@*]

Selecting Several Paths


Path Expression //book/title | //book/price //title | //price /bookstore/book/title | //price Result Selects all the title AND price elements of all book elements Selects all the title AND price elements in the document Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

XPath Operators
Operator | Description Computes two node-sets Addition Subtraction Multiplication Division Equal Example //book | //cd Return value Returns a nodeset with all book and cd elements 10 2 24 2 true if price is 9.80 false if price is 9.90

+ * div =

6+4 6-4 6*4 8 div 4 price=9.80

!= < <= > >= or and mod

Not equal Less than Less than or equal to Greater than Greater than or equal to or and Modulus (division remainder)

price!=9.80 price<9.80 price<=9.80 price>9.80 price>=9.80 price=9.80 or price=9.70 price>9.00 and price<9.90 5 mod 2

true if price is 9.90 false if price is 9.80 true if price is 9.00 false if price is 9.80 true if price is 9.00 false if price is 9.90 true if price is 9.90 false if price is 9.80 true if price is 9.90 false if price is 9.70 true if price is 9.80 false if price is 9.50 true if price is 9.80 false if price is 8.50 1

Loading
var

the XML Document

xmlhttp=new XMLHttpRequest() var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")


Selecting

Nodes

xmlDoc.evaluate(xpath,

xmlDoc, null, XPathResult.ANY_TYPE,null); xmlDoc.selectNodes(xpath);

Das könnte Ihnen auch gefallen