`
dingjun1
  • 浏览: 213576 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

xpath语法

阅读更多
转载:http://www.w3schools.com/XPath/xpath_syntax.asp
暂时没有整理,乱,以后再整理
Selecting Nodes
/
Selects from the root node

//
Selects nodes in the document from the current node that match the selection no matter where they are


XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:
Expression Description
nodename Selects all child nodes of the named 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

In the table 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 or a node that contains a specific value.

Predicates are always embedded in square brackets.

In the table 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.

Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
/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

XPath 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

In the table 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

Selecting Several Paths

By using the | operator in an XPath expression you can select several paths.

In the table below we have listed some path expressions and the result of the expressions:
Path Expression Result
//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
分享到:
评论

相关推荐

    C#操作xml之xpath语法

    ### C#操作XML之XPath语法详解 在C#中,操作XML文档是一项常见的任务,尤其在处理配置文件、数据交换格式或集成不同系统时。为了有效地解析和查询XML数据,XPath成为了一种不可或缺的语言。本文将深入探讨XPath的...

    Python3 xml.etree.ElementTree支持的XPath语法详解

    通过这些基本语法,我们可以编写XPath表达式来精确地查找XML文档中的特定部分。例如,在给定的XML文档中,我们可以找到每个国家的名称: ```python root = ET.fromstring(xml_string) for country in root.findall...

    XPath语法详细介绍..XPath语法详细介绍

    ### XPath语法详细介绍 #### XPath简介 XPath是一种用于在XML文档中进行导航的查询语言。它提供了简洁、高效的方式来定位和提取文档中的特定部分。XPath不仅仅适用于XML文档的搜索,还是XSLT(一种用于转换XML文档...

    xpath语法与函数

    ### XPath语法与函数知识点 #### XPath简介 XPath是一种在XML文档中查找信息的语言。它主要用于在XML文档中通过元素和属性进行导航。XPath是XSLT标准中的关键组成部分,同时也是XQuery和XPointer的基础。 #### ...

    XPath 语法(MSDN)

    XPath语法中的关键概念包括: 1. **路径表达式**:如同文件系统的路径,XPath使用路径来定位节点。例如,`/bookstore/book`表示从文档根开始选取`&lt;bookstore&gt;`元素下的所有`&lt;book&gt;`元素。 2. **运算符和特殊字符**...

    Xpath 语法生成器(插件).zip

    XPath语法生成器是一种实用工具,尤其是对于开发人员和数据分析师来说,它能简化在网页抓取过程中定位元素的工作。 这个名为“XPath Helper”的插件是专门为浏览器设计的,它使得用户能够在浏览网页时实时生成和...

    Xpath语法.docx

    Xpath语法.docx

    xpath的语法

    XPath语法的核心概念包括: 1. **节点类型**:XPath中的节点包括元素(element)、属性(attribute)、文本(text)、命名空间(namespace)、处理指令(processing-instruction)、注释(comment)和文档...

    XPath语法规则中文WORD版最新版本

    本文档主要讲述的是XPath 语法规则;XPath使用路径表达式去确定XML文档中的节点。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看

    XPath语法简要说明1

    XPath语法简要说明1

    XPath语法详解xml开发必备

    下面我们将详细探讨XPath语法及其在XML开发中的应用。 1. **XPath的基本概念** - **节点**:XML文档由一系列节点构成,如元素(element)、属性(attribute)、文本(text)、命名空间(namespace)、处理指令...

    xpath 的语法

    XPath 语法 XPath(XML Path Language)是一种用于选择 XML 文档中节点的语言。它提供了一种灵活的方式来定位和选择 XML 文档中的节点,从而能够快速地访问和处理 XML 文档。 在 XPath 中,节点是以树形结构组织的...

    xpath语法详解

    XPath的语法基础: 1. **节点类型**:XPath定义了七种基本的节点类型,包括元素(element)、属性(attribute)、文本(text)、命名空间(namespace)、处理指令(processing-instruction)、注释(comment)以及...

    Python-astpath一个命令行工具用于使用XPath语法查询PythonASTs

    astpath:一个命令行工具用于使用XPath语法查询Python ASTs

    Python爬虫基础之XPath语法与lxml库的用法详解

    主要给大家介绍了关于Python爬虫基础之XPath语法与lxml库用法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    XPath语法20130920

    XPath语法 XPath(XML Path Language)是一种用于选择XML文档中节点或节点集的语言。它使用路径表达式来选取节点,节点是通过沿着路径(path)或者步(steps)来选取的。 XPath的基本概念: * 节点:XML文档中的...

    xpath 语法测试

    总结来说,"xpath 语法测试"提供了一个实践XPath的环境,结合Web服务、jQuery和AJAX,展示了XPath在现代Web开发中的应用。通过这个测试,开发者可以提升XPath技能,更有效地处理XML数据,提高代码的效率和可读性。

    Xpath生成器,自动生成XPATH,C#版

    XPath(XML Path Language)是一种在XML文档中查找信息的语言,它是W3C组织制定的一种标准查询语言,用于选取XML文档中的节点,包括元素、属性、文本等。在本项目“Xpath生成器,自动生成XPATH,C#版”中,开发者...

    XPath教程

    本教程将深入探讨XPath的核心概念、语法以及实际应用。 XPath的主要目标是提供一种简洁而有效的机制,使得XML文档的某一部分可以被快速定位。在XSL(Extensible Stylesheet Language,可扩展样式表语言)中,XPath...

Global site tag (gtag.js) - Google Analytics