转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 电脑应用 >> 电脑技术 >> 正文
XPath Tutorial-from w3schools.com         ★★★

XPath Tutorial-from w3schools.com

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3421 更新时间: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]  下一页


[系统软件]给IE增加dom3xpath支持  [VB.NET程序]VBCOM TUTORIAL(3)
[VB.NET程序]VBCOM TUTORIAL(2)  [VB.NET程序]VBCOM TUTORIAL(1)
[VB.NET程序]Microsoft Agent Tutorial Chapter 2  [VB.NET程序]Microsoft Agent Tutorial Chapter 1
[Web开发]XSLT轻松入门第四章:XPath的语法  [Web开发]XPath 2.0 的新特性
[Web开发]JDOM/XPATH编程指南  [Web开发].NET中的xpath
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

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

    同类栏目
    · 电脑技术  · 操作系统
    · 磁盘工具  · 视音频技术
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

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

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台