一、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 变量由 “$” 并跟随一个名称来进行定义,举例,$bookstore
- xquery 注释被 (: 和 :) 分割,例如,(: 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/book where $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/book where $x/price>30 order by $x/title return $x/title
上面的 xquery 表达式的结果:
<title lang="en">learning xml</title> <title lang="en">xquery kick start</title>
到此这篇关于xml基本概念xpath、xslt与xquery函数的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持代码网。
发表评论