Xpath教程-zt

上一篇 / 下一篇  2009-03-31 16:23:23 / 个人分类:selenium

 

转自:http://www.cnblogs.com/caoxch/archive/2006/11/17/563804.html

http://www.w3school.com.cn/xpath/xpath_syntax.asp

XPath教程

XPath是用于在XML文件上寻找信息的一种语言。
XPath用于操纵XML文件上的元素和属性。
W3C的XSLT标准中XQuery和XPointer都是在XPath表达式的基础上建立起来的
因此学习有关XPath对于提高XML的使用技巧是很有帮助的

XPath 参考

可以找到内置于 XPath 2.0, XQuery 1.0 和XSLT 2.0的完整函数参考

XPath 函数

内容目录

XPath 介绍
This chapter explains what XPath is.
关于XPath的概念

XPath 点
This chapter defines the different types of nodes in XPath and the relationship of nodes.
在XPath不同类型的点,以及点与点的联系

XPath 语法
This chapter explains the XPath syntax.
有关XPath的语法介绍

XPath Axes
This chapter explains the XPath axes.
这章介绍了XPath轴

XPath Operators
This chapter lists the operators that can be used in XPath expressions.
列举了可以用在XPath中的操作符

XPath 实例
This chapter uses the "books.xml" document to demonstrate some XPath examples.
使用了"books.xml"文档来演示一些XPath例子

XPath 摘要
This chapter contains a summary on what you have learned in this tutorial and a recommendation on what subject you should study next.
回顾了所学的内容,并推荐下一步应该学习的内容。

XPath介绍
w3pop.com / 2006-09-19

XPath Nodes(节点)

XPath is a language for finding information in an XML document. XPath is used to navigate through elements and attributes in an XML document.
XPath是用于在XML文件上寻找信息的一种语言。XPath用于操纵XML文件上的元素和属性。


What You Should Already Know
写在前面

Before you continue you should have a basic understanding of the following:
首先你要了解以下几个概念:

  • HTML / XHTML
  • XML / XML Namespaces
  • XML / XML命名空间

If you want to study these subjects first, find the tutorials on ourHome page.
你可以从我们的首页获取相关的教程


What is XPath?
什么是可扩展路径(XPath)?

  • XPath is a syntax for defining parts of an XML document
  • XPath 是XML中的一种语法
  • XPath uses path expressions to navigate in XML documents
  • XPath 是XML文档中的路径索引
  • XPath contains a library of standard functions
  • XPath 包含一个标准函数库
  • XPath is a major element in XSLT
  • XPath 是一个XSLT的主要元素
  • XPath is a 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 a traditional computer file system.
XPath路径表达式用于从XML文档中选取节点或节点设置。这些语句类似于传统操作系统的语句。


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 a major element in the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.
XPath是XSLT标准的一个重要元素。在创建XSLT文档前你必须先了解XPath。

You can read more about XSLT in ourXSLT tutorial.
你可以从我们的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表达式构建的。它们有着相同的数据模式并支持相同的函数和操作。

You can read more about XQuery in ourXQuery tutorial.
你可以从我们的XQuery教程中了解更多。


XPath is a W3C Standard
XPath 是W3C标准

XPath became a W3C Recommendation 16. November 1999.
XPath 在1999年11月16日成为W3C标准。

XPath was designed to be used by XSLT, XPointer and other XML parsing software.

You can read more about the XPath standard in ourW3C tutorial.
你可以从我们的W3C教程中了解更多。

XPath Nodes(节点)
w3pop.com / 2006-09-21

XPath介绍XPath 语法

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
在XPath里,有7中不同的节点:元素,属性,文本,名称空间,处理指令,内容,文档(根目录root)节点。


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).
在XPath里,有7中不同的节点:元素,属性,文本,名称空间,处理指令,内容,文档(根)节点,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)”属性值

Atomic values are nodes with no children or parent.
“单元素”属性值只没有子节点和父节点。

Example of atomic values:
“单元素(Atomic)”属性值例子:

J 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.
节点元素可拥有任意个数的子类。

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
祖类

A 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:
在下面例子里,title元素的“祖类元素”是book元素和bookstore元素。

<bookstore>
<book>
<title>Harry Potter</title>

<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>

Descendants
下属类

A 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 语法
w3pop.com / 2006-09-21

XPath Nodes(节点)XPath Axes

XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a 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 a path or steps. The most useful path expressions are listed below:
XPath通过路径表达式在XML文档中选取节点。可以通过一条语句或相应的步骤选取一个节点。下面列出了最常使用的路径表达式:

Expression
表达式
Description
注释
nodenameSelects 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:
在下面的表格中,我们列出了一些路径表达式及其运行的结果:

Path Expression
表达式
Result
结果
bookstoreSelects all the child nodes of the bookstore element
选取bookstore元素的所有子节点
/bookstoreSelects the root element bookstore
选取bookstore元素的根节点

Note:If the path starts with a slash ( / ) it always represents an absolute path to an element!
注意:如果一个路径以(/)开始,那么它必须是表述该元素所在的绝对路径

bookstore/bookSelects all book elements that are children of bookstore
选取bookstore中的所有book子元素
//bookSelects all book elements no matter where they are in the document
选取文档中的所有book元素
bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
选取文档中所有处于bookstore节点下的book元素
//@langSelects all attributes that are named lang
选取所有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.
通常使用方括号[ ]来指定条件。

Examples
实例

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
选取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 a value of 'eng'
选取所有lang属性值为‘eng’的title元素
/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节点下的所有包含price元素大于35.00的book元素
/bookstore/book[price>35.00]/titleSelects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00
选取bookstore节点下的所有包含price元素大于35.00的book节点下的title元素


Selecting Unknown Nodes
选取未知节点

XPath wildcards can be used to select unknown XML elements.
可以通过 XPath 通配符选取未知的XML元素。

Wildcard
通配符
Description
注释
*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:
在下面的表格中我们列出了一些表达式及其运行结果:

Path Expression
表达式
Result
结果
/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元素


Selecting Several Paths
选取多个路径

By using the | operator in an XPath expression you can select several paths.
你可以同过在表达式中添加 | 来选取多个路径。

Examples
实例

In the table below we have listed some path expressions and the result of the expressions:
在下面的表格中我们列出了一些表达式及其运行结果:

Path Expression
表达式
Result
结果
//book/title | //book/priceSelects all the title AND price elements of all book elements
选取book节点中的所有title和price元素
//title | //priceSelects all the title AND price elements in the document
选取文档中的所有title和price元素
/bookstore/book/title | //priceSelects all the title elements of the book element of the bookstore element AND all the price elements in the document
选取bookstore节点下的book节点中的所有title元素和文档中所有的price元素

XPath Axes
w3pop.com / 2006-09-21

XPath 语法XPath 操作符

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>


XPath Axes
XPath 方法

An axis defines a node-set relative to the current node.
用于定义与单前节点相关的属性。

AxisName
方法名称
Result
结果
ancestorSelects all ancestors (parent, grandparent, etc.) of the current node
选取单前节点的祖类(父类,父类的父类及更多)
ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
选取当前节点的祖类(父类,父类的父类及更多)和节点自身
attributeSelects all attributes of the current node
选取当前节点的所有属性
childSelects all children of the current node
选取当前节点的所有子类
descendantSelects all descendants (children, grandchildren, etc.) of the current node
选取当前节点的所有下属类(子类,子类的子类及更多)
descendant-or-selfSelects all descendants (children, grandchildren, etc.) of the current node and the current node itself
选取当前节点的所有下属类(子类,子类的子类及更多)和节点自身
followingSelects everything in the document after the closing tag of the current node
选取文档中当前节点结束标签前的所有元素
following-siblingSelects all siblings after the current node
选取位于当前节点后的所有同属类节点
namespaceSelects all namespace nodes of the current node
选取与当前节点相名的命名空间
parentSelects the parent of the current node
选取当前节点的父节点
precedingSelects everything in the document that is before the start tag of the current node
选取文档中所有位于当前节点开始标签前的元素
preceding-siblingSelects all siblings before the current node
选取位于当前节点前的所有同属类节点
selfSelects the current node
选取当前节点


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 steps, each separated by a slash:
绝对路径表达式以(/)开始,两种路径表达式都包含一个或多个层次,用 / 分隔:

An absolute location path:
绝对路径:
/step/step/...
A relative location path:
相对路径:
step/step/...

Each step is evaluated against the nodes in the current node-set.
每个层次都对应当前节点的一个属性。

A step consists of:
层包括:

  • an axis (defines the tree-relationship between the selected nodes and the current node)
  • 层路径(取决于目标节点与当前节点间的目录关联)
  • a node-test (identifies a node within an axis)
  • 节点测试(确认节点包含此属性)
  • zero or more predicates (to further refine the selected node-set)
  • 无条件或条件(进一步检索节点属性)

The syntax for a location step is:
选取的语法为:

axisname::nodetest[predicate]

Examples
实例

Example
实例
Result
结果
child::bookSelects all book nodes that are children of the current node
选取当前节点的所有book子节点
attribute::langSelects the lang attribute of the current node
选取当前节点的所有lang属性值
child::*Selects all children of the current node
选取当前节点的所有子节点
attribute::*Selects all attributes of the current node
选取当前节点的所有属性值
child::text()Selects all text child nodes of the current node
选取当前节点下的所有text节点的子节点
child::node()Selects all child nodes of the current node
选取当前节点的所有子节点
descendant::bookSelects all book descendants of the current node
选取当前节点下所有book节点的下属类
ancestor::bookSelects all book ancestors of the current node
选取当前节点下所有book的祖类
ancestor-or-self::bookSelects all book ancestors of the current node - and the current as well if it is a book node
选取所有当前节点下的book祖类和book节点自身
child::*/child::priceSelects all price grandchildren of the current node
选取所有当前节点的子节点中所包含的price元素

XPath 操作符
w3pop.com / 2006-09-21

XPath AxesXPath 实例

An XPath expression returns either a node-set, a string, a Boolean, or a number.
XPath 表达式返回值可以是:节点属性,字符型,布尔型,数字型。


XPath Operators
XPath 操作符

Below is a list of the operators that can be used in XPath expressions:
下面给出了XPath表达式所支持的所有操作符:

Operator
操作符
Description
注释
Example
实例
Return value
返回值
|Computes two node-sets
连接两条语句
//book | //cdReturns a node-set with all book and cd elements
返回所有book和cd的节点属性
+Addition
6 + 410
-Subtraction
6 - 42
*Multiplication

6 * 4

24
divDivision
8 div 42
=Equal
price=9.80true if price is 9.80
false if price is 9.90
返回true或false
!=Not equal
不等
price!=9.80true if price is 9.90
false if price is 9.80
返回true或false
<Less than
小于
price<9.80true if price is 9.00
false if price is 9.80
返回true或false
<=Less than or equal to
小于等于
price<=9.80true if price is 9.00
false if price is 9.90
返回true或false
>Greater than
大于
price>9.80true if price is 9.90
false if price is 9.80
返回true或false
>=Greater than or equal to
大于等于
price>=9.80true if price is 9.90
false if price is 9.70
返回true或false
ororprice=9.80 or price=9.70true if price is 9.80
false if price is 9.50
逻辑连接,判断两个条件中的一个
andandprice>9.00 and price<9.90true if price is 9.80
false if price is 8.50
逻辑连接,同时判断两个条件
modModulus (division remainder)
求余
5 mod 21

XPath 实例
w3pop.com / 2006-09-21

XPath 操作符XPath 摘要

Let's try to learn some basic XPath syntax by looking at some examples.
让我们通过一些实例来学习一些基础的XPath语法。


The XML Example Document
XML文档

We will use the following XML document in the examples below.
我们将在下面的例子中引用这个XML文档。

"books.xml"文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>

<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>

<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>

<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>

<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>

<price>39.95</price>
</book>
</bookstore>

在浏览器中查看"books.xml"文件


Selecting Nodes
选取节点

We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:
我们会用Microsoft XMLDOM object来载入XML文档,用selectNodes()方法来从XML文档中选择节点。

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"

xmlDoc.load("books.xml")
xmlDoc.selectNodes(path expression)


Select all book Nodes
选取所有book节点

The following example selects all the book nodes under the bookstore element:
下面的实例演示如何选取booksotre节点的所有book子元素:

xmlDoc.selectNodes("/bookstore/book")

如果你的IE是5.0以上版本,你可以自己尝试一下!


Select the First book Node
选取第一个book节点

The following example selects only the first book node under the bookstore element:
下面的例子只选择了bookstore节点的第一个book子元素:

xmlDoc.selectNodes("/bookstore/book[0]")

如果你的IE是5.0以上版本,你可以自己尝试一下

Note:IE 5 and 6 has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
注意:IE5和IE6把[0]作为第一节点,但是在W3C标准中第一个节点是[1]!!

Note:This is corrected in IE 6 SP2!
注意:IE6 SP2 已经更正!


Select the prices
选取prices

The following example selects the text from all the price nodes:
下面的例子演示了如何从所有的price节点获取文本:

xmlDoc.selectNodes("/bookstore/book/price/text()")

如果你的IE是5.0以上版本,你可以自己尝试一下


Selecting price Nodes with Price>35
选取price>35的price节点

The following example selects all the price nodes with a price higher than 35:
下面的例子演示了如何选取price值大于35的price节点:

xmlDoc.selectNodes("/bookstore/book[price>35]/price")

如果你的IE是5.0以上版本,你可以自己尝试一下


Selecting title Nodes with Price>35
选取price>35的titile节点

The following example selects all the title nodes with a price higher than 35:
下面的例子演示了如何选取price属性值大于35的title节点:

xmlDoc.selectNodes("/bookstore/book[price>35]/title")

如果你的IE是5.0以上版本,你可以自己尝试一下

XPath 摘要
w3pop.com / 2006-09-21

XPath 实例函数参考

XPath Summary
XPath概要

This tutorial has taught you how to find information in an XML document.
你已经学会了如何从一个XML文档中选取你所需要的信息。

You have learned how to use XPath to navigate through elements and attributes in an XML document.
你已经学会如何使用XPath语句从XML文档中寻找元素和属性。

You have also learned how to use some of the standard functions that is built-in in XPath.
你也学会了如何使用XPath的内置函数。

For more information on XPath, please look at ourXPath Reference.
想了解更多的XPath,请查看我们的XPath参考。


Now You Know XPath, What's Next?
进一步学习XPath

The next step is to learn about XSLT, XQuery, XLink, and XPointer.
下一步我们将学习XSLT,XQuery,XLink和XPointer。

XSLT

XSLT is the style. sheet language for XML files.
XSLT是XML文件的样式语言。

With XSLT you can transform. XML documents into other formats, like XHTML.
你可以用XSLT将XML文档转换成其他格式,例如XHTML。

If you want to learn more about XSLT, please visit ourXSLT tutorial.
如果你要了解更多关于XSLT,请阅读XSLT教程。

XQuery

XQuery is about querying XML data.
XQuery是XML数据的查询语句。

XQuery is designed to query anything that can appear as XML, including databases.
XQuery可以查询任何可以用XML呈现的数据,包括数据库

If you want to learn more about XQuery, please visit ourXQuery tutorial.
如果你要了解更多关于XQuery,请阅读XQuery教程。

XLink and XPointer
XLink 和 XPointer

Linking in XML is divided into two parts: XLink and XPointer.
XML中的链接分两种:XLink和XPointer。

XLink and XPointer define a standard way of creating hyperlinks in XML documents.
可以使用XLink和XPointer在XML文档中创建一个标准的超级链接。

If you want to learn more about XLink and XPointer, please visit ourXLink and XPointer tutorial.
如果你要了解更多关于XLink和XPointer,请阅读XLink和XPointer教程。

函数参考
w3pop.com / 2006-09-21

XPath 摘要

The following reference library defines the functions required for XPath 2.0, XQuery 1.0 and XSLT 2.0.
下面的参考目录库详细包括了XPath 2.0, XQuery 1.0 和 XSLT 2.0.所要求的函数


Functions Reference
函数参考目录

  • Accessor
  • Error and Trace
  • Numeric
  • String
  • AnyURI
  • Boolean
  • Duration/Date/Time
  • QName
  • Node
  • Sequence
  • Context

The default prefix for the function namespace is fn:, and the URI is:
http://www.w3.org/2005/02/xpath-functions.
函数命名空间的默认前缀是“fn”,URI是:http://www.w3.org/2005/02/xpath-functions.

Accessor 函数

Name
名称
Description
描述
fn:node-name(node)Returns the node-name of the argument node
返回自变量节点的节点(node)名称
fn:nilled(node)Returns a Boolean value indicating whether the argument node is nilled
返回一个逻辑值,用来指明自变量(argument)节点是否被拒绝
fn:data(item.item,...)Takes a sequence of items and returns a sequence of atomic values
获取项目的一个序列并返回一个原子值(atomic value)序列
fn:base-uri()
fn:base-uri(node)
Returns the value of the base-uri property of the current or specified node
返回当前节点或指定节点内基于uri属性的值
fn:document-uri(node)Returns the value of the document-uri property for the specified node
返回指定节点中uri文档属性的值

Error 和 Trace 函数

Name
名称
Description
描述
fn:error()
fn:error(error)
fn:error(error,description)
fn:error(error,description,error-object)
Example案例: error(fn:QName('http://example.com/test', 'err:toohigh'), 'Error: Price is too high')

Result结果: Returns http://example.com/test#toohigh and the string "Error: Price is too high" to the external processing environment

fn:trace(value,label)Used to debug queries
用来调试查询(queries)

Numeric 函数

Name
名称
Description
描述
fn:number(arg)Returns the numeric value of the argument. The argument could be a boolean, string, or node-set

Example: number('100')
Result: 100
返回自变量数值。这个自变量可以是一个逻辑值(boolean)、字符串(string)或者是节点设置
举例: number('100')
结果: 100

fn:abs(num)Returns the absolute value of the argument
返回自变量的绝对值

Example举例: abs(3.14)
Result结果: 3.14

Example举例: abs(-3.14)
Result结果: 3.14

fn:ceiling(num)Returns the smallest integer that is greater than the number argument

Example: ceiling(3.14)
Result: 4

fn:floor(num)Returns the largest integer that is not greater than the number argument
返回大于自变量数值的最小整数

Example举例: floor(3.14)
Result结果: 3

fn:round(num)Rounds the number argument to the nearest integer
返回小于自变量数值的最大整数

Example举例: round(3.14)
Result结果: 3

fn:round-half-to-even()Example举例: round-half-to-even(0.5)
Result结果: 0

Example举例: round-half-to-even(1.5)
Result结果: 2

Example举例: round-half-to-even(2.5)
Result结果: 2

Strings 函数

Name
名称
Description
描述
fn:string(arg)Returns the string value of the argument. The argument could be a number, boolean, or node-set
返回自变量的字符串值。这个自变量可以是一个数字、逻辑值或节点设置。

Example举例: string(314)
Result结果: "314"

fn:codepoints-to-string(int,int,...)Returns a string from a sequence of code points
从一组代码点(code points)中返回一个字符串

Example举例: codepoints-to-string(84, 104, 233, 114, 232, 115, 101)
Result结果: 'Thérèse'

fn:string-to-codepoints(string)Returns a sequence of code points from a string
从字符串中返回一组连续的代码点(code points)

Example举例: string-to-codepoints("Thérèse")
Result结果: 84, 104, 233, 114, 232, 115, 101

fn:codepoint-equal(comp1,comp2)Returns true if the value of comp1 is equal to the value of comp2, according to the Unicode code point collation (http://www.w3.org/2005/02/xpath-functions/collation/codepoint), otherwise it returns false
根据统一字符编码点整理,如果comp1值与comp2值等同,则返回true。(http://www.w3.org/2005/02/xpath-functions/collation/codepoint)反之则为false
fn:compare(comp1,comp2)
fn:compare(comp1,comp2,collation)
Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to comp2, or 1 if comp1 is greater than comp2 (according to the rules of the collation that is used)
如果comp1少于comp2则返回-1,如果comp1等同于comp2或comp1
大于comp2(根据整理规则)则返回0。 

Example举例: compare('ghi', 'ghi')
Result结果: 0

fn:concat(string,string,...)Returns the concatenation of the strings
返回字符串的串联

Example举例: concat('XPath ','is ','FUN!')
Result结果: 'XPath is FUN!'

fn:string-join((string,string,...),sep)Returns a string created by concatenating the string arguments and using the sep argument as the separator
返回一个字符串,此字符串是通过连接字符串自变量和使用解析器形式的sep自变量来创建的。

Example举例: string-join(('We', 'are', 'having', 'fun!'), ' ')
Result结果: ' We are having fun! '

Example举例: string-join(('We', 'are', 'having', 'fun!'))
Result结果: 'Wearehavingfun!'

Example举例:string-join((), 'sep')
Result结果: ''

fn:substring(string,start,len)
fn:substring(string,start)
Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end
输出一个从开始位置算起,指定长度的子字符串。第一字符索引是1.如果长度被忽略,那么就输出一个从开始位置起一直到末尾的子链。

Example举例: substring('Beatles',1,4)
Result结果: 'Beat'

Example举例: substring('Beatles',2)
Result结果: 'eatles'

fn:string-length(string)
fn:string-length()
Returns the length of the specified string. If there is no string argument it returns the length of the string value of the current node
输出指定字符串的长度。如果没有字符串自变量则返回当前节点的字符串值的长度。

Example举例: string-length('Beatles')
Result结果: 7

fn:normalize-space(string)
fn:normalize-space()
Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. If there is no string argument it does the same on the current node
从指定字符串中删除前端的空间与末尾的空间,用一个空格替代所有的内部空白序列。如果没有字符串自变量则在当前节点中进行上述操作。

Example举例: normalize-space(' The   XML ')
Result结果: 'The XML'

fn:normalize-unicode() 
fn:upper-case(string)Converts the string argument to upper-case
把字符串自变量转换到upper-case 

Example举例: upper-case('The XML')
Result结果: 'THE XML'

fn:lower-case(string)Converts the string argument to lower-case
把字符串转自变量换到lower-case 

Example举例: lower-case('The XML')
Result结果: 'the xml'
fn:translate(string1,string2,string3)Converts string1 by replacing the characters in string2 with the characters in string3
通过用字符串3中的字符串替换字符串2中的字符,转换字符串1

Example举例: translate('12:30','30','45')
Result结果: '12:45'

Example举例: translate('12:30','03','54')
Result结果: '12:45'

Example举例: translate('12:30','0123','abcd')
Result结果: 'bc:da'

fn:escape-uri(stringURI,esc-res)Example举例: escape-uri("http://example.com/test#car", true())
Result结果: "http%3A%2F%2Fexample.com%2Ftest#car"

Example举例: escape-uri("http://example.com/test#car", false())
Result结果: "http://example.com/test#car"

Example举例: escape-uri ("http://example.com/~bébé", false())
Result结果: "http://example.com/~b%C3%A9b%C3%A9"

fn:contains(string1,string2)Returns true if string1 contains string2, otherwise it returns false
如果string 1含有string 2则输出true;反之为false

Example举例: contains('XML','XM')
Result结果: true

fn:starts-with(string1,string2)Returns true if string1 starts with string2, otherwise it returns false
如果string 1以string 2开头则输出true;反之则为false

Example举例: starts-with('XML','X')
Result结果: true

fn:ends-with(string1,string2)Returns true if string1 ends with string2, otherwise it returns false
如果string 1以string 2为结尾则输出true;反之为false

Example举例: ends-with('XML','X')
Result结果: false

fn:substring-before(string1,string2)Returns the start of string1 before string2 occurs in it
在string2触发之前输出 string1的始端

Example举例: substring-before('12/10','/')
Result结果: '12'

fn:substring-after(string1,string2)Returns the remainder of string1 after string2 occurs in it
在string2触发之前输出string1的余数

Example举例: substring-after('12/10','/')
Result结果: '10'

fn:matches(string,pattern)Returns true if the string argument matches the pattern, otherwise, it returns false
如果字符串自变量与模式相匹配则输出true,反之为false

Example举例: matches("Merano", "ran")
Result结果: true

fn:replace(string,pattern,replace)Returns a string that is created by replacing the given pattern with the replace argument
输出一个字符串(用替换自变量替换特定模式)

Example举例: replace("Bella Italia", "l", "*")
Result结果: 'Be**a Ita*ia'

Example举例: replace("Bella Italia", "l", "")
Result结果: 'Bea Itaia'
fn:tokenize(string,pattern)Example举例: tokenize("XPath is fun", "\s+")
Result结果: ("XPath", "is", "fun")

anyURI 函数

Name
名称
Description
描述
fn:resolve-uri(relative,base) 

Boolean 函数

Name
名称
Description
描述
fn:boolean(arg)Returns a boolean value for a number, string, or node-set
输出一个数字、字符串及节点设置的逻辑函数
fn:not(arg)The argument is first reduced to a boolean value by applying the boolean() function. Returns true if the boolean value is false, and false if the boolean value is true
自变量是第一个成为适用于boolean()函数的逻辑值。如果逻辑值是false则输出true,反之亦然。

Example举例: not(true())
Result结果: false

fn:true()Returns the boolean value true
输出逻辑值为true

Example举例: true()
Result结果: true

fn:false()Returns the boolean value false
输出逻辑值为false

Example举例: false()
Result结果: false

Durations、Dates 和 Times 函数

Component Extraction Functions on Durations, Dates and Times
持续时间、日期、时间函数组件


TAG: 教程

 

评分:0

我来说两句

Name
名称
Description
描述
fn:dateTime(date,time)Converts the arguments to a date and a time
把自变量转换成日期和时间
fn:years-from-duration(datetimedur)Returns an integer that represents the years component in the canonical lexical representation of the value of the argument
输出一个在自变量值标准词汇表示中表示年份的整形
fn:months-from-duration(datetimedur)Returns an integer that represents the months component in the canonical lexical representation of the value of the argument
输出一个在自变量值标准词汇表示中表示月份的整形
fn:days-from-duration(datetimedur)Returns an integer that represents the days component in the canonical lexical representation of the value of the argument
输出一个在自变量值标准词汇表示中表示天数的整形
fn:hours-from-duration(datetimedur)Returns an integer that represents the hours component in the canonical lexical representation of the value of the argument
输出一个在自变量值标准词汇表示中表示时数的整形
fn:minutes-from-duration(datetimedur)Returns an integer that represents the minutes component in the canonical lexical representation of the value of the argument
输出一个在自变量值标准词汇表示中表示分数的整形
fn:seconds-from-duration(datetimedur)Returns a decimal that represents the seconds component in the canonical lexical representation of the value of the argument
输出一个在自变量值标准词汇表示中表示秒数的小数
fn:year-from-dateTime(datetime)Returns an integer that represents the year component in the localized value of the argument
输出一个在自变量本地值中表示年份的整形

Example举例: year-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result结果: 2005

fn:month-from-dateTime(datetime)Returns an integer that represents the month component in the localized value of the argument
输出一个在自变量本地值中表示月份的整形

Example举例: month-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result结果: 01

fn:day-from-dateTime(datetime)Returns an integer that represents the day component in the localized value of the argument
输出一个在自变量本地值中表示的整形

Example举例: day-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result结果: 10

fn:hours-from-dateTime(datetime)Returns an integer that represents the hours component in the localized value of the argument
输出一个在自变量本地值中表示时数的整形

Example举例: hours-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result结果: 12

fn:minutes-from-dateTime(datetime)Returns an integer that represents the minutes component in the localized value of the argument
输出一个在自变量本地值中表示分数的整形

Example举例: minutes-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))
Result结果: 30

fn:seconds-from-dateTime(datetime)Returns a decimal that represents the seconds component in the localized value of the argument
输出一个在自变量本地值中表示秒数的小数

Example举例: seconds-from-dateTime(xs:dateTime("2005-01-10T12:30:00-04:10"))
Result结果: 0

fn:timezone-from-dateTime(datetime)Returns the time zone component of the argument if any
输出任何自变量时区组件如果它们存在
fn:year-from-date(date)Returns an integer that represents the year in the localized value of the argument
输出一个在自变量本地值中表示年份的整形

Example举例: year-from-date(xs:date("2005-04-23"))
Result结果: 2005

fn:month-from-date(date)Returns an integer that represents the month in the localized value of the argument
输出一个在自变量本地值中表示月份的整形

Example举例: month-from-date(xs:date("2005-04-23"))
Result结果: 4

fn:day-from-date(date)Returns an integer that represents
« 2024-04-26  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 1095612
  • 日志数: 260
  • 文件数: 1
  • 书签数: 1
  • 建立时间: 2009-01-05
  • 更新时间: 2017-08-22

RSS订阅