XML基本概念XPath、XSLT与XQuery函数介绍

XML基本概念XPath、XSLT与XQuery函数介绍

一、XPath查询

XSL指扩展样式表语言(EXtensible Stylesheet Language)。

官方网站:https://www.w3.org/TR/xpath/

XSL - 不仅仅是样式表语言,包括三部分:

XSLT :一种用于转换 XML 文档的语言。XPath :一种用于在 XML 文档中导航的语言。XSL-FO :一种用于格式化 XML 文档的语言。

XPath

XML DOM使用XPath解析HTML。HTMLAgilityPack使用XPath解析HTML (JumonyHTML采用CSS3方式解析HTML)。

1、选取节点

XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。

下面列出了最有用的路径表达式:

nodename:选取此节点的所有子节点。

/:从根节点选取。

//:选择文档中的节点,而不考虑它们的位置。

. :选取当前节点。

.. :选取当前节点的父节点。

@ :选取属性。

实例:

bookstore : 选取 bookstore 元素的所有子节点。

/bookstore:选取根元素 bookstore。
注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!

bookstore/book:选取属于 bookstore 的子元素的所有 book 元素。

//book:选取所有 book 子元素,而不管它们在文档中的位置。

bookstore//book:选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什幺位置。

//@lang:选取名为 lang 的所有属性。

2、谓语(Predicates)

谓语用来查找某个特定的节点或者包含某个指定的值的节点。谓语被嵌在方括号中。

实例:

/bookstore/book[1] : 选取属于 bookstore 子元素的第一个 book 元素。

/bookstore/book[last()] : 选取属于 bookstore 子元素的最后一个 book 元素。

/bookstore/book[last()-1] : 选取属于 bookstore 子元素的倒数第二个 book 元素。

/bookstore/book[position() 3] : 选取最前面的两个属于 bookstore 元素的子元素的 book 元素。

//title[@lang] : 选取所有拥有名为 lang 的属性的 title 元素。

//title[@lang= eng ] : 选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。

/bookstore/book[price 35.00] : 选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00。

/bookstore/book[price 35.00] /title: 选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00。

book[count(author)=2] : 有两个author元素的Book

book/*[starts-with(name(), B )] : 以B开头的并在book下的元素。

book/*[contains(name(), B )] : 包含 B 名称的元素

book/*[string-length(name())==3] : 元素的名称长度为3

选择含有某子元素的元素://table[.//img]

3、选取未知节点

XPath 通配符可用来选取未知的 XML 元素。

* : 匹配任何元素节点。

@* : 匹配任何属性节点。

node() : 匹配任何类型的节点。

实例:

/bookstore/*: 选取 bookstore 元素的所有子元素。

//*: 选取文档中的所有元素。

//title[@*]: 选取所有带有属性的 title 元素。

4、选取若干路径

通过在路径表达式中使用 | 运算符,您可以选取若干个路径。

实例:

//book/title | //book/price : 选取 book 元素的所有 title 和 price 元素。

//title | //price : 选取文档中的所有 title 和 price 元素。

/bookstore/book/title | //price : 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。

5、XPath 轴(Axes)

轴可定义相对于当前节点的节点集。

ancestor : 选取当前节点的所有先辈(父、祖父等)。

ancestor-or-self: 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。

attribute : 选取当前节点的所有属性。

child: 选取当前节点的所有子元素。

descendant: 选取当前节点的所有后代元素(子、孙等)。

descendant-or-self: 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。

following: 选取文档中当前节点的结束标签之后的所有节点。

namespace: 选取当前节点的所有命名空间节点。

parent: 选取当前节点的父节点。

preceding: 选取文档中当前节点的开始标签之前的所有节点。

preceding-sibling: 选取当前节点之前的所有同级节点。

self: 选取当前节点。

步的语法:轴名称::节点测试[谓语]

实例:

child::book: 选取所有属于当前节点的子元素的 book 节点。

attribute::lang: 选取当前节点的 lang 属性。

child::*: 选取当前节点的所有子元素。

attribute::*: 选取当前节点的所有属性。

child::text(): 选取当前节点的所有文本子节点。

child::node(): 选取当前节点的所有子节点。

descendant::book: 选取当前节点的所有 book 后代。

ancestor::book: 选择当前节点的所有 book 先辈。

ancestor-or-self::book: 选取当前节点的所有 book 先辈以及当前节点(如果此节点是 book 节点)

child:: * /child::price: 选取当前节点的所有 price 孙节点。

6、XPath 运算符

下面列出了可用在 XPath 表达式中的运算符:

| : 计算两个节点集+: 加法-: 减法*: 乘法div: 除法=: 等于!=: 不等于 : 小于 =: 小于或等于 : 大于 =: 大于或等于or: 或and: 与mod: 计算除法的余数

二、XSLT

指 XSL 转换。在此教程中,你将学习如何使用 XSLT 将 XML 文档转换为其他文档,比如 XHTML。

XSLT 使用 XPath 在 XML 文档中进行导航。

1、样式表声明

把文档声明为 XSL 样式表的根元素是 或 。

注释: 和 是完全同义的,均可被使用!

根据 W3C 的 XSLT 标准,声明 XSL 样式表的正确方法是:

xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

或者:

xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

如需访问 XSLT 的元素、属性以及特性,我们必须在文档顶端声明 XSLT 命名空间。

2、创建 XSL 样式表

然后创建一个带有转换模板的 XSL 样式表( cdcatalog.xsl ):

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 table border="1" tr bgcolor="#9acd32" th align="left" Title /th th align="left" Artist /th /tr xsl:for-each select="catalog/cd" tr td xsl:value-of select="title"/ /td td xsl:value-of select="artist"/ /td /tr /xsl:for-each /table /body /html /xsl:template /xsl:stylesheet

3、把 XSL 样式表链接到 XML 文档

向 XML 文档( cdcatalog.xml )添加 XSL 样式表引用:

?xml version="1.0" encoding="ISO-8859-1"? ?xml-stylesheet type="text/xsl" href="cdcatalog.xsl" rel="external nofollow" ? catalog cd title Empire Burlesque /title artist Bob Dylan /artist country USA /country company Columbia /company price 10.90 /price year 1985 /year /cd cd title Hide your heart /title artist Bonnie Tyler /artist country UK /country company CBS Records /company price 9.90 /price year 1988 /year /cd /catalog

如果您使用的浏览器兼容 XSLT,它会很顺利地把您的 XML 转换为 XHTML。(注意本地文件没效果,要放到Web服务器上才能转换)

4、XSL元素

1、 元素

XSL 样式表由一个或多套被称为模板(template)的规则组成。

每个模板含有当某个指定的节点被匹配时所应用的规则。

XSL 样式表由一个或多套被称为模板(template)的规则组成。

每个模板含有当某个指定的节点被匹配时所应用的规则。

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 table border="1" tr bgcolor="#9acd32" th Title /th th Artist /th /tr tr td . /td td . /td /tr /table /body /html /xsl:template /xsl:stylesheet

元素定义了一个模板。而 match= / 属性则把此模板与 XML 源文档的根相联系。

元素内部的内容定义了写到输出结果的 HTML 代码。

2、 元素

元素用于提取某个选定节点的值,并把值添加到转换的输出流中:

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 table border="1" tr bgcolor="#9acd32" th Title /th th Artist /th /tr tr td xsl:value-of select="catalog/cd/title"/ /td td xsl:value-of select="catalog/cd/artist"/ /td /tr /table /body /html /xsl:template /xsl:stylesheet

注释:select 属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。

这个例子的结果有一点缺陷:仅有一行数据从 XML 文档被拷贝到输出结果。

可以使用 元素来循环遍历 XML 元素,并显示所有的记录。

3、 元素

元素可用于选取指定的节点集中的每个 XML 元素。

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 table border="1" tr bgcolor="#9acd32" th Title /th th Artist /th /tr xsl:for-each select="catalog/cd" tr td xsl:value-of select="title"/ /td td xsl:value-of select="artist"/ /td /tr /xsl:for-each /table /body /html /xsl:template /xsl:stylesheet

结果过滤

通过在 元素中添加一个选择属性的判别式,我们也可以过滤从 XML 文件输出的结果。

xsl:for-each select="catalog/cd[artist='Bob Dylan']"

合法的过滤运算符:

= (等于)!= (不等于) (小于) (大于)

4、 元素

元素用于对结果进行排序。

如需对结果进行排序,只要简单地在 XSL 文件中的 元素内部添加一个 元素:

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 table border="1" tr bgcolor="#9acd32" th Title /th th Artist /th /tr xsl:for-each select="catalog/cd" xsl:sort select="artist"/ tr td xsl:value-of select="title"/ /td td xsl:value-of select="artist"/ /td /tr /xsl:for-each /table /body /html /xsl:template /xsl:stylesheet

5、 元素

元素用于放置针对 XML 文件内容的条件测试。

如需添加有条件的测试,请在 XSL 文件中的 元素内部添加 元素:

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 table border="1" tr bgcolor="#9acd32" th Title /th th Artist /th /tr xsl:for-each select="catalog/cd" xsl:if test="price 10" tr td xsl:value-of select="title"/ /td td xsl:value-of select="artist"/ /td /tr /xsl:if /xsl:for-each /table /body /html /xsl:template /xsl:stylesheet

6、 元素

元素用于结合 和 来表达多重条件测试。可以包含多个 元素

要插入针对 XML 文件的多重条件测试,请向 XSL 文件添加 、 以及 :

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 table border="1" tr bgcolor="#9acd32" th Title /th th Artist /th /tr xsl:for-each select="catalog/cd" tr td xsl:value-of select="title"/ /td xsl:choose xsl:when test="price 10" td bgcolor="#ff00ff" xsl:value-of select="artist"/ /td /xsl:when xsl:otherwise td xsl:value-of select="artist"/ /td /xsl:otherwise /xsl:choose /tr /xsl:for-each /table /body /html /xsl:template /xsl:stylesheet

7、 元素

元素可把一个模板应用于当前的元素或者当前元素的子节点。

假如我们向 元素添加一个 select 属性,此元素就会仅仅处理与属性值匹配的子元素。我们可以使用 select 属性来规定子节点被处理的顺序。

请看下面的 XSL 样式表:

?xml version="1.0" encoding="iso-8859-1"? xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:template match="/" html body h2 My CD Collection /h2 xsl:apply-templates/ /body /html /xsl:template xsl:template match="cd" p xsl:apply-templates select="title"/ xsl:apply-templates select="artist"/ /p /xsl:template xsl:template match="title" Title: span style="color:#ff0000" xsl:value-of select="."/ /span br / /xsl:template xsl:template match="artist" Artist: span style="color:#00ff00" xsl:value-of select="."/ /span br / /xsl:template /xsl:stylesheet

8、 元素

元素用于声明局部或全局的变量。

注释:如果被声明为顶层元素,则该变量是全局的,而如果在模板内声明,则变量是本地的。 一旦您设置了变量的值,就无法改变或修改该值!

提示:您可以通过 元素的内容或通过 select 属性,向变量添加值!

下面的例子通过 元素的内容为变量 header 赋值:

?xml version="1.0" encoding="ISO-8859-1"? xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:variable name="header" tr th Element /th th Description /th /tr /xsl:variable xsl:template match="/" html body table xsl:copy-of select="$header" / xsl:for-each select="reference/record" tr xsl:if category="XML" td xsl:value-of select="element"/ /td td xsl:value-of select="description"/ /td /xsl:if /tr /xsl:for-each /table br / table xsl:copy-of select="$header" / xsl:for-each select="table/record" tr xsl:if category="XSL" td xsl:value-of select="element"/ /td td xsl:value-of select="description"/ /td /xsl:if /tr /xsl:for-each /table /body /html /xsl:template /xsl:stylesheet

三、XQuery

XQuery 相对于 XML的作用,等同于 SQL 相对于数据库。

XQuery 被设计用来查询 XML 数据。 不仅仅限于 XML 文件,还包括任何可以 XML 形态呈现的数据,包括数据库。

SQLServer XML类型使用基于XPath的XQuery。

XQuery 也被称为 XML Query。

1、XQuery 的基础语法规则:

XQuery 对大小写敏感XQuery 的元素、属性以及变量必须是合法的 XML 名称。XQuery 字符串值可使用单引号或双引号。XQuery 变量由 $ 并跟随一个名称来进行定义,举例,$bookstoreXQuery 注释被 (: 和 :) 分割,例如,(: XQuery 注释 :)

2、FLWOR 表达式

FLWOR 是 For, Let, Where, Order by, Return 的只取首字母缩写。

for 语句把 bookstore 元素下的所有 book 元素提取到名为 $x 的变量中。where 语句选取了 price 元素值大于 30 的 book 元素。order by 语句定义了排序次序。将根据 title 元素进行排序。return 语句规定返回什幺内容。在此返回的是 title 元素。

下面这个路径表达式可选取 bookstore 元素下的 book 元素下所有的 title 元素,并且其中的 price 元素的值必须大于 30。:

doc("books.xml")/bookstore/book[price 30]/title --doc() 用于打开 "books.xml" 文件:

下面这个 FLWOR 表达式所选取的数据和上面的路径表达式是相同的:

for $x in doc("books.xml")/bookstore/bookwhere $x/price 30 return $x/title

上面的 XQuery 表达式结果是:

title lang="en" XQuery Kick Start /title title lang="en" Learning XML /title

通过 FLWOR,您可以对结果进行排序:

for $x in doc("books.xml")/bookstore/bookwhere $x/price 30order by $x/titlereturn $x/title

上面的 XQuery 表达式的结果:

title lang="en" Learning XML /title title lang="en" XQuery Kick Start /title

XML基本概念XPath、XSLT与XQuery函数介绍