xpath简介__用于在XML中查找

XPath介绍

XPath  is  language  for  finding  information  in  an  XML  document.  XPath  is  used  to  navigate  through  elements  and  attributes  in  an  XML  document.
Xpath是一种能够在XML文档中寻找信息的语言。它通过XML文档中的元素和属性来进行导航。 


--------------------------------------------------------------------------------

What You Should Already Know
你应具备的知识
Before you continue you should have basic understanding of the following:
在你继续前你应该具备以下知识的基础:

HTML XHTML 
XML XML Namespaces[命名空间] 

--------------------------------------------------------------------------------

What is XPath?
什么是XPath?
XPath is syntax for defining parts of an XML document
XPath 是针对XML文档部分内容定义的语法 
XPath uses path expressions to navigate in XML documents
XPath 使用路径表达式在XML文档中导航 
XPath contains library of standard functions
XPath 包含了一系列标准函数 
XPath is major element in XSLT
XPath 在XSLT当中是一个主要的元素 
XPath is W3C Standard
XPath是W3C标准 

--------------------------------------------------------------------------------

XPath Path Expressions
XPath 路径表达式
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with traditional computer file system.
XPath 使用路径表达式来选择XML文档中的nodes(节)或是node-set(节集)。这些路径表达式看上去与你平时所见的传统计算机文件系统路径非常地相似。


--------------------------------------------------------------------------------

XPath Standard Functions
XPath 标准函数
XPath includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more.
XPath包含了数量超过100的内置函数。这些函数针对字符串值,数字值,日期和时间比较,节操作,顺序操作,布尔值,等等。 


--------------------------------------------------------------------------------

XPath is Used in XSLT
XPath被用在XSLT
XPath is major element in the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.
在XSLT标准中XPath是主要的元素。没有XPath知识你将很难建立XSLT文档。

XQuery and XPointer are both built on XPath expressions. XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. 
XQuery和XPointer都建立于XPath表达式。XQuery 1.0 和 XPath 2.0共享相同的数据模型并支持相同的函数和操作


--------------------------------------------------------------------------------

XPath is W3C Standard
XPath是W3C标准
XPath became W3C Recommendation 16. November 1999.
XPat于1999年11月16日成为W3C的推荐标准

XPath was designed to be used by XSLT, XPointer and other XML parsing software.
XPath被设计成为用语XSLT,XPoniter以及其他XML解析的软件。

 

 

XPath Nodes(节)

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
在XPath中有七种nodes(节):元素,属性,文字,命名空间,处理说明,注释,和文档(根)节。


--------------------------------------------------------------------------------

XPath Terminology
XPath术语
Nodes/节
In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).
XML文档被视为数状的节。树的根部被称为文档的节(或根节)。

Look at the following XML document:
观察下面的XML文档:

<?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>
Example of nodes in the XML document above:
上面举例的XML文档的节有:

<bookstore>  (document node)
<author>J K. Rowling</author>  (element node)
lang="en"  (attribute node)
Atomic values
原子值
Atomic values are nodes with no children or parent.
原子值是那些没有子或父的节(无上下关系)。

Example of atomic values:
举例中的原子值:

K. Rowling
"en"
Items
项目
Items are atomic values or nodes.
项目是原子值或节。


--------------------------------------------------------------------------------

Relationship of Nodes
节之间的关系
Parent/父
Each element and attribute has one parent.
每个元素和属性有一父亲。

In the following example; the book element is the parent of the title, author, year, and price:
下面的举例中:book元素是title,author,year和price的父亲

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
Children/子
Element nodes may have zero, one or more children.
元素节可能有0个或多个子

In the following example; the title, author, year, and price elements are all children of the book element:
下面的举例中:title,author,year和price元素都是book元素的子元素

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
Siblings/兄
Nodes that have the same parent.
指那些有相同父的

In the following example; the title, author, year, and price elements are all siblings:
下面的举例中title, author, year, 和 price元素都为兄弟

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
Ancestors/祖
node's parent, parent's parent, etc.
节的父,父的父....都为祖

In the following example; the ancestors of the title element are the book element and the bookstore element:
下面的举例中:book元素和bookstore元素都为title元素的祖元素

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>
Descendants/孙
node's children, children's children, etc.
节的子,子的子...都为孙

In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:
下面的举例中:bookstore元素的孙有book,title,author,year以及price元素

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>
XPath语法

XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following path or steps.
XPath使用路径表达式来选择XML文档的节或是节集。顺着路径或步骤来选择节。


--------------------------------------------------------------------------------

The XML Example Document
XML实例文档
We will use the following XML document in the examples below.
举例中我们将使用下面的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>

--------------------------------------------------------------------------------

Selecting Nodes
选择节
XPath uses path expressions to select nodes in an XML document. The node is selected by following path or steps. The most useful path expressions are listed below:
一些非常有用的路径表达式: 

表达式 描述 
nodename Selects all child nodes of the node[选择所有目前节的子节] 
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[选择属性] 

Examples
实例
In the table below we have listed some path expressions and the result of the expressions:
下面我们所列举的表格有路径表达式以及其结果:

路径表达式 结果 
bookstore Selects all the child nodes of the bookstore element[选择所有bookstore元素的子节] 
/bookstore Selects the root element bookstore 
Note: If the path starts with slash it always represents an absolute path to an element!

[选择了bookstore的根元素。注意:如果路径的开始为(/)那此路径一定是到该元素的绝对路径] 
bookstore/book Selects all book elements that are children of bookstore[选择了所有在bookstore的子元素book元素所包含的所有元素(其实就为bookstore里book元素所包含的元素)] 
//book Selects all book elements no matter where they are in the document[选择所有为book元素的内容而不管book元素处于何处(有不同的父也没关系)] 
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element[在bookstore元素内所有含有book元素的元素内容(只要book元素的祖元素为bookstore元素那都符合条件)] 
//@lang Selects all attributes that are named lang[选择所有属性名为lang的属性] 


--------------------------------------------------------------------------------

Predicates
谓语
Predicates are used to find specific node or node that contains specific value.
谓语用来指定明确的节所含有的特殊的值

Predicates are always embedded in square brackets.
谓语被嵌入在中括号

Examples
举例
In the table below we have listed some path expressions with predicates and the result of the expressions:
下面的表格列举了一些使用了谓语的路径表达式以及其产生的结果:

路径表达式 结果 
/bookstore/book[1]  Selects the first book element that is the child of the bookstore element[选择了bookstore里的第一个book元素] 
/bookstore/book[last()] Selects the last book element that is the child of the bookstore element[选择bookstore里最后一个book元素] 
/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element[bookstore中倒数第二个book元素] 
/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element[在bookstore中前两个book元素] 
//title[@lang] Selects all the title elements that have an attribute named lang[选择所有含有lang属性的title元素] 
//title[@lang='eng'] Selects all the title elements that have an attribute named lang with value of 'eng'[选择所有含有lang属性并且值为eng的title元素] 
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have price element with value greater than 35.00[选择所有bookstore中book元素里price元素内容大于35.00的book元素] 
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have price element with value greater than 35.00[选择bookstore中book的子元素title,并且其兄弟元素price的内容得大于35.00] 


--------------------------------------------------------------------------------

Selecting Unknown Nodes
选择未知的节
XPath wildcards can be used to select unknown XML elements.
XPath的通配符可以用来选择未知的XML元素

通配符 描述 
Matches any element node[相吻合的所有元素节] 
@* Matches any attribute node[相吻合的所有属性节] 
node() Matches any node of any kind[吻合任何类型的节] 

Examples实例
In the table below we have listed some path expressions and the result of the expressions:
下面的表格我们将列举一些路径表达式以及它们的结果

路径表达式 结果 
/bookstore/* Selects all the child nodes of the bookstore element[选择所有bookstore的子节] 
//* Selects all elements in the document[选择所有文档中的元素] 
//title[@*] Selects all title elements which have any attribute[选择元素为title并且其含有属性] 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值