学习XPath

XPath是一种用来从XML文档中提取信息的语言


所需知识:

  • HTML / XML
  • XML / XML Namespace

什么是XPath:

  • XPath is a syntax for defining parts of  an XML document
  • XPath uses path expression 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中有7类元素:

  • element
  • attribute
  • text
  • namespace
  • processing-instruction
  • comment
  • document nodes

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

<bookstore>
  <book>
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore> 

如下是节点的例子:

<bookstore>是根元素节点

<author>J K. Rowling</author>是元素节点

lang="en"是属性节点


atomic value(原子节点)指没有孩子节点或父亲节点的节点

原子节点的例子:

J K. Rowling

"en"


节点之间的关系-parent:每个元素和属性都有一个父节点

节点之间的关系-children:每个元素可以有0个,1个或多个孩子节点

节点之间的关系-siblings:兄弟节点——有共同的父亲节点的节点

节点之间的关系-ancestors:节点的父节点,父节点的父节点.....

节点之间的关系-descendents:节点的孩子节点,孩子节点的孩子节点......


XPath使用路径表达式来在XML文档中选择一个节点或一组节点

 <?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>

路径表达式:

nodename - select all nodes with the name "nodename"

/ - select from the root node

// - select nodes from the document from the current node that match the selection no matter where they are

. - select the current node

.. - select the parent node of the current node

@ - select attribute


Predicates are used to find a specific node or a node that contains a specific value.

/bookstore/book[1] - 选择bookstore的第一个book孩子元素

/bookstore/book[last()] - 选择bookstore的最后一个book孩子元素

/bookstore/book[last() - 1] - 选择bookstore的倒数第二个book孩子元素

/bookstore/book[position() < 3] - 选择前两个book孩子元素

//title[@lang] - 选择所有的包含lang属性的title元素

//title[@lang='eng'] - 选择所有lang属性值为eng的title元素

/bookstore/book[price > 0.35] - 选择bookstore下所有price属性大雨0.35的book元素

/bookstore/book[price > 0.35]/title - 选择所有price大于0.35的book元素下的所有title元素


XPath通配符可以用来选择未知的XML元素:

* - 匹配任何元素节点

@* - 匹配任何属性节点

node() - 匹配任何类型的节点

/bookstore/* - 匹配bookstore下的所有孩子节点

//* - 选择文档中的所有节点

//title[@*] - 选择所有包含属性的title元素


//book/title | //book/ - 选择book元素的title元素和book元素

//title | //price - 选择所有的title元素和price元素

/bookstore/book/title | //price - 选择bookstore下的book下的所有title元素和所有price元素

An XPath axis defines a node set relative to the current node

ancestor选择当前节点的祖先节点
ancestor-or-self选择当前节点的祖先节点和当前节点
attribute选择当前节点的所有属性
child选择当前节点的所有孩子节点
descendant选择当前节点的所有子孙节点
descendant-or-self选择当前节点的所有子孙节点和自己
following选择当前节点后面的所有节点
following-sibling选择当前节点后面的所有兄弟节点
namespace选择当前节点的所有命名空间节点
parent选择当前节点的父亲节点
preceding选择当前节点之前的节点
preceding-sibling选择当前节点之前的兄弟节点
self当前节点

child::bookselects all book nodes that are children of the current node
attribute::langselects the lang attribute of the current node
child::*selects all element children of the current node
attribute::*selects all attributes of the current node
child::text()selects all text node children of the current node
child::node()selects all children of the current node
descendant::bookselects all descendant of the current node
ancestor::bookselects all book ancestors of the current node
ancestor-or-self::bookselects all book ancestors of the current node and the current as well if it is a book node
child::*/child::priceselects all price grandchildren of the current node

XPath Operators

|, +, -, *, div,  =, !=, <, <=, >, >=, or, and, mod


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值