转至繁体中文版     | 网站首页 | 文章中心 | 下载中心 | 图片中心 | 笑话频道 | 教程频道 | 会员中心 | 雁过留声 | 
最新公告:     "MinTao学以致用网"欢迎您的光临,你的支持便是我们的动力,欢迎广大网友和各界人士亲临指导,你们的一个小小的建议便是我们发展的开路石!  [MinTao  2007年9月5日]        
您现在的位置: MinTao学以致用网 >> 文章中心 >> 电子课堂 >> 电脑应用 >> 电脑技术 >> 文章正文
专题栏目
更多内容
最新推荐 更多内容
相关文章
没有相关文章
更多内容
XPath Tutorial-from w3schools.com           ★★★
XPath Tutorial-from w3schools.com
作者:佚名 文章来源:ofaq 点击数: 更新时间:2006-7-27
fied by the @ prefix.

This XPath expression selects all attributes named country:

//@country

This XPath expression selects all cd elements which have an attribute named country:

//cd[@country]

This XPath expression selects all cd elements which have any attribute:

//cd[@*]

This XPath expression selects all cd elements which have an attribute named country with a value of ''''UK'''':

//cd[@country=''''UK'''']



XPath Location Paths

Previous Next

A location path expression results in a node-set.


Location Path Expression

A location path can be absolute or relative.

An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more location steps, each separated by a slash:

An absolute location path:
/step/step/...
A relative location path:
step/step/...

The location steps are evaluated in order one at a time, from left to right. Each step is evaluated against the nodes in the current node-set. If the location path is absolute, the current node-set consists of the root node. If the location path is relative, the current node-set consists of the node where the expression is being used. Location steps consist of:

  • an axis (specifies the tree relationship between the nodes selected by the location step and the current node)
  • a node test (specifies the node type and expanded-name of the nodes selected by the location step)
  • zero or more predicates (use expressions to further refine the set of nodes selected by the location step)

The syntax for a location step is:

axisname::nodetest[predicate]

Example:

child::price[price=9.90]


Axes and Node Tests

An axis defines a node-set relative to the current node. A node test is used to identify a node within an axis. We can perform a node test by name or by type.

AxisName Description ancestor Contains all ancestors (parent, grandparent, etc.) of the current node

Note: This axis will always include the root node, unless the current node is the root node

ancestor-or-self Contains the current node plus all its ancestors (parent, grandparent, etc.) attribute Contains all attributes of the current node child Contains all children of the current node descendant Contains all descendants (children, grandchildren, etc.) of the current node

Note: This axis never contains attribute or namespace nodes

descendant-or-self Contains the current node plus all its descendants (children, grandchildren, etc.) following Contains everything in the document after the closing tag of the current node following-sibling Contains all siblings after the current node

Note: If the current node is an attribute node or namespace node, this axis will be empty

namespace Contains all namespace nodes of the current node parent Contains the parent of the current node preceding Contains everything in the document that is before the starting tag of the current node preceding-sibling Contains all siblings before the current node

Note: If the current node is an attribute node or namespace node, this axis will be empty

self Contains the current node

Examples

Example Result child::cd Selects all cd elements that are children of the current node (if the current node has no cd children, it will select an empty node-set) attribute::src Selects the src attribute of the current node (if the current node has no src attribute, it will select an empty node-set) child::* Selects all child elements of the current node attribute::* Selects all attributes of the current node child::text() Selects the text node children of the current node child::node() Selects all the children of the current node descendant::cd Selects all the cd element descendants of the current node ancestor::cd Selects all cd ancestors of the current node ancestor-or-self::cd Selects all cd ancestors of the current node and, if the current node is a cd element, the current node as well child::*/child::price Selects all price grandchildren of the current node / Selects the document root

Predicates

A predicate filters a node-set into a new node-set. A predicate is placed inside square brackets ( [ ] ).

Examples

Example Result child::price[price=9.90] Selects all price elements that are children of the current node with a price element that equals 9.90 child::cd[position()=1] Selects the first cd child of the current node child::cd[position()=last()] Selects the last cd child of the current node child::cd[position()=last()-1] Selects the last but one cd child of the current node child::cd[position()<6] Selects the first five cd children of the current node /descendant::cd[position()=7] Selects the seventh cd element in the document child::cd[attribute::type="classic"] Selects all cd children of the current node that have a type attribute with value classic

Location Path Abbreviated Syntax

Abbreviations can be used when describing a location path.

The most important abbreviation is that child:: can be omitted from a location step.

Abbr Meaning Example none child:: cd is short for child::cd @ attribute:: cd[@type="classic"] is short for
child::cd[attribute::type="classic"] . self::node() .//cd is short for
self::node()/descendant-or-self::node()/child::cd .. parent::node()

../cd is short for
parent::node()/child::cd

// /descendant-or-self::node()/ //cd is short for
/descendant-or-self::node()/child::cd

Examples

Example Result cd Selects all the cd elements that are children of the current node * Selects all child elements of the current node text() Selects all text node children of the current node @src Selects the src attribute of the current node @* Selects all the attributes of the current node cd[1] Selects the first cd child of the current node cd[last()] Selects the last cd child of the current node */cd Selects all cd grandchildren of the current node /book/chapter[3]/para[1] Selects the first para of the third chapter of the book //cd Selects all the cd descendants of the document root and thus selects all cd elements in the same document as the current node . Selects the current node .//cd Selects the cd element descendants of the current node .. Selects the parent of the current node ../@src Selects the src attribute of the parent of the current node cd[@type="classic"] Selects all cd children of the current node that have a type attribute with value classic cd[@type="classic"][5] Selects the fifth cd child of the current node that has a type attribute with value classic cd[5][@type="classic"] Selects the fifth cd child of the current node if that child has a type attribute with value classic cd[@type and @country] Selects all the cd children of the current node that have both a type attribute and a country attribute

XPath Expressions

Previous Next

X

上一页  [1] [2] [3] [4] 下一页

文章录入:mintao    责任编辑:mintao 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007 MinTao学以致用网(www.mintao.net) Inc All Rights Reserved.
    QQ:543098146有事请Q我! QQ:261561092有事请Q我 QQ:179647303有事请Q我 MSN:min906@126.com
    站长:MinTao 信息产业部ICP备案号:鄂ICP备07500065号

    学以致用是我们学习者的至高境界和不懈追求,[MinTao学以致用网]与大家共同学习,共同进步……
    信息产业部备案
    *鄂ICP备07500065号