---
title: "XSLT2.0----XPATH2.0序列"
author: "persistC"
date: "2009-06-06T16:24:17Z"
tags: []
slug: "xslt2-0-xpath2-0"
draft: false
lastmod: "2009-06-06T16:25:06Z"
id: 403259
url: "https://persistc.iteye.com/blog/403259"
---

严格说来XPATH2.0和XSLT2.0并无直接联系，但是XPATH的主要应用环境是在XSLT中了，所以也把这点内容写在XSLT中。

XPATH2.0中引入了sequence(序列的概念)。注意是XPATH2.0不是XSLT2.0。

XPATH表达式在1.0的时候只能表示四种类型：

1,节点集。

&lt;xsl:vale-of select="root/item"/&gt;

2,布尔值。

&lt;xsl:value-of select="true()"/&gt;

&lt;xsl:if test="root/item=1"/&gt;

3,数值型

&lt;xsl:vlaue-of  select="number('12.01')"/&gt;

&lt;xsl:value-of  select="root/item\[1]"/&gt;中的1.

4,字符串

&lt;xsl:value-of  select="'ABCD'"/&gt;

当XPATH2.0引入了对XML Schema内建数据类型的支持，使用户能直接访问19种简单类型。

XPATH2.0将每个xpath表达式的返回值都看作一个序列。

举例来说：我们可以写这样一条语句，&lt;xsl:value-of select="1,2,3"/&gt;。其中，(1,2,3)是一个序列。再如：&lt;xsl:value-of select="root/item,1,root/item\[1],root/itm\[3]"/&gt;,select属性指定了一个序列，其中包括root/item序列（嵌套序列），数字1，root/item\[1]节点，root/item\[3]节点。

 

序列仍可作为xslt2.0函数的参数，跟xpath1.0表达式一样：

for $x in /order/item return $x/price * $x/quantity

注意，这是以个XPATH2.0的表达式，不是XSLT2.0的。它返回一个序列，即所有item的price(价格)与quantity(数量)的乘积的集合。我们可以将序列作为XSLT2.0函数的参数，如：

&lt;xsl:value-of select="sum(for $x in /order/item return $x/price * $x/quantity)"/&gt;

 

&lt;xsl:value-of select="if (number(catalog/cd\[1]/price) &lt; number(catalog/cd\[2]/price)) then

                                     number(catalog/cd\[1]/price else catalog/cd\[2]"/&gt;

这是一个if操作，求两个cd中价格低的那个，同样是xapth2.0的表达式。

注意和xslt2.0的&lt;xsl:if&gt;的区别。

 

xpath1.0中&lt;xsl:if test="students/student/name='Fred'/&gt;表示，只要有一个学生的名字是Fred，那么表达式就为真。

xpath2.0中提供了更为准确的运算。

some $x in students/student/name satisfies $x='Fred'

另外：

every $x in /students/student/name satisfies $x='Fred'表示所有的学生的名字都是Fred，才为真。