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 样式表的正确方法是:
1 | < xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" > |
或者:
1 | < 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>
您可能感兴趣的文章
- 09-22Xml简介_动力节点Java学院整理
- 09-22编写xml没有代码提示的解决方法
- 09-22Xpath语法格式总结
- 09-22xpath简介_动力节点Java学院整理
- 09-22XML基本概念XPath、XSLT与XQuery函数介绍
- 09-22简单了解XML 树结构
- 09-22XPath的定义、基础语法、使用示例和高级用法
- 09-22XML实体注入深入理解
阅读排行
推荐教程
- 09-22简单了解XML 树结构
- 09-22编写xml没有代码提示的解决方法
- 09-22XML实体注入深入理解
- 09-22xpath简介_动力节点Java学院整理
- 09-22Xpath语法格式总结
- 09-22XML基本概念XPath、XSLT与XQuery函数介绍
- 09-22Xml简介_动力节点Java学院整理
- 09-22XPath的定义、基础语法、使用示例和高级用法