Home > Coding > XPath

XPath

//a[@class=’PinImage ImgLink’]
(Selects all ‘a’ elements that have the attribute ‘class’ with a value of ‘PinImage ImgLink’)

忽發奇想要探究Yahoo Pipes,遇見XPath無解,唯有臨陣磨槍,邊邊做。幸好有一點兒HTML以及Javascript以及jQuery的底子,大部分概念如Parent、Siblings可以觸類旁通。

XPath is used to navigate through elements and attributes in an XML document.

Selecting Nodes
nodename Selects all nodes with the name “nodename”
/ 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
bookstore Selects all nodes with the name “bookstore”
/bookstore Selects the root element bookstore
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
/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

Selecting Unknown Nodes
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
/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

Selecting Several Paths
//book/title | //book/price Selects all the title AND price elements of all book elements
//title | //price Selects all the title AND price elements in the document
/bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

簡化如下:
/ CHILD
// DESCENDANT
.. PARENTS
* ALL
@ ATTRIBUTE
” STRING
| AND
//elements[@attribute=’value’]

Categories: Coding
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: