XPath Syntax
XML Path Language uses a declarative notation rather than a
procedural one. Each expression developed by this notation describes the types
of nodes to match based on the hierarchical relationship between the nodes.
A D V E R T I S E M E N T
For
example, the expression "book/author" means find the author element contained
in the book element.
XML Example
One XML document is given below.
<?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>
Selecting Nodes
To select nodes XPath uses path expressions. The node is
selected by following a path. The most useful path expressions are:-
Expression
Description
nodename
Selects all child nodes of the 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
Example
Table given below we have listed some path expressions and the
result of the expressions:
Path Expression
Result
bookstore
Selects all the child nodes of the bookstore element
/bookstore
Selects the root element bookstore
Note: If the path starts with a slash ( / ) it always
represents an absolute path to an element!
bookstore/book
Selects all book elements that are children of
bookstore
//book
Selects all book elements no matter where they are in
the document
bookstore//book
Selects all book elements that are descendant of the
bookstore element, no matter where they are under the bookstore element
//@lang
Selects all attributes that are named lang
Predicates
Predicates are used to find a specific node that
contains a value.Predicates are always embedded in
square brackets.
Example
Table given below we have listed some path expressions with predicates and
the result of the expressions:
Path 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='eng']
Selects all the title elements that have an attribute
named lang with a value of 'eng'
/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
How we selecting unknown nodes?
Wildcards can be used to select unknown XML elements.
Wildcard
Description
*
Matches any element node
@*
Matches any attribute node
node()
Matches any node of any kind
Example
Table given below we have listed some path expressions and the
result of the expressions:
Path Expression
Result
/bookstore/*
Selects all the child nodes of the bookstore element
//*
Selects all elements in the document
//title[@*]
Selects all title elements which have any attribute
Be the first one to comment on this page.
Share And Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords: XPath Syntax,
xpath query syntax,
xsl syntax,
xpath vb net,
xslt syntax,
asp net xpath,
xpath starts with,
xquery syntax,
vb net syntax,
xpath case insensitive,
asp net syntax,
xpath reference,
xpath tutorial,
xpath examples,
xml syntax,
xpath namespace,
xpath expressions,
xpath c#,
xpath namespaces,
xpath xml,
cdata syntax,
xpath functions,
selectsinglenode xpath,
xpath example,
c# syntax,
xsl xpath,
xpath query,
dtd syntax,
xpath attribute,
xpath contains,
xpath xslt,
xpath xmlns,
xquery xpath,
xpath expression,
javascript xpath,
xpath sort,
stylesheet syntax,
xpath selectnodes,
xpath attributes,
xpath name,
xpath queries,
xpath dom4j,
xhtml syntax,
xpath html,
xpath cdata,
xsd syntax,
xpath predicates,
xpath w3c,
css syntax,
html syntax,
xpath count,
xpath text,
xpath axis,
xmldocument xpath,
uri syntax,
xpath or,
xpath xalan,
javascript syntax,
xpath document,
jdom xpath,
xpath perl,
xpath position,
xpath predicate,
xpath substring,
xpath sample,
xpath sql,
namespace syntax,
examples syntax,
perl syntax,
msxml xpath