xsl:template 相当于函数,而
<xsl:template match="/">
</xsl:template>
相当于一个主函数。
使用xsl:apply-templates/>继续广度深层的遍历,然后之后写的函数对其中遍历到的数据进行匹配,若没有函数进行分析则就直接输出了。
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
匹配时,对于一个任意的数据,主函数调用之后的任意函数进行匹配,如果有,则使用其中的函数的操作进行操作,没有就直接输出了。
template称作模版,但是其实作用相当于函数。其中可以进行容易的输出,以实现xml的转换操作,模版中可以进行更深层的遍历 使用“xsl:apply-template“,可以调用函数使用xsl:call-template,也可以使用xsl:copy 或者xsl:copy-of进行节点内容的输出,或者使用xsl:value-of进行属性值或者其他值的输出,可以使用xsl:for-each进行条件的遍历,可以使用xsl:if进行条件判断,实用xsl:sort进行排序操作。大致上xslt进行的操作就这些。
xslt中的常用属性有以下几种
match属性是匹配路径,进行任意xml中内容的匹配。matci="/"则为匹配根节点,即实现main函数,也就是所有之后操作的接口。
name属性是名称,使用xsl:call-template可以调用固定名的模版。
mode 模式属性,对名称进行更进一步的区分操作。
priority 优先级属性,依然是对名称进行进一步区分,调用模版时,调用优先级较高的模版
as 返回类型属性,声明模版的返回值类型。
select xpath属性,对进行判断或遍历的操作 使用xpath来规定其选取的对象。就是使用xpath来选择一定的元素或其他属性。
以下有一些使用的用例:
作业中输出book中的所有的section。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="section">
<xsl:copy-of select="."/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="title"></xsl:template>
<xsl:template match="author"/>
<xsl:template match="p"/>
</xsl:stylesheet>
这里对根节点进行遍历,然后去匹配所有的其他元素,使其不输出,然后对section进行 xsl:copy-of 进行的元素输出,然后对section再进行遍历,使嵌套在section中的其他section元素也能够进行输出。
如何同时对多个xml进行操作:
<xsl:variable name="doc" select="document('book.xml')"/>
在这里声明一个文件,为载入的另一个文件,然后在遍历原xml的同时,对新加入的xm也进行遍历l
<xsl:template match="/">
<xsl:apply-templates/>
<xsl:apply-templates select="$doc/node()"/>
</xsl:template>
q5:
使用参数传值,实现判断操作:
<xsl:variable name="doc" select="document('reviews.xml')"/>
<xsl:template match="/">
<book-with-prices>
<xsl:apply-templates select="//book"/>
</book-with-prices>
</xsl:template>
<xsl:template match="book">
<xsl:apply-templates select="./price"></xsl:apply-templates>
</xsl:template>
<xsl:template name="K">
<xsl:param name="bookprice"/>
<xsl:param name="booktitle"/>
<xsl:for-each select="$doc//entry[./title/text()=$booktitle]">
<book-with-prices>
<title>
<xsl:value-of select="./title/text()"/>
</title>
<price-bstore2>
<xsl:value-of select="./price/text()"/>
</price-bstore2>
<price-bstore1><xsl:value-of select="$bookprice"/></price-bstore1>
</book-with-prices>
</xsl:for-each>
</xsl:template>
<xsl:template match="price">
<xsl:call-template name="K"><xsl:with-param name="bookprice" select="text()"/>
<xsl:with-param name="booktitle" select="../title/text()"/>
</xsl:call-template>
</xsl:template>
使用xpath进行一系列判断,然后快速选中需要的元素
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<book>
<xsl:apply-templates select="//book[count(./child::author)>0]"/>
</book>
</xsl:template>
<xsl:template match="book">
<book>
<xsl:copy-of select="./title"/>
<xsl:copy-of select="./child::element()[position()=2]"/>
<xsl:copy-of select="./child[position()=3]"/>
<xsl:if test="count(./child::author)>2">
<et-al/>
</xsl:if>
</book>
</xsl:template>
</xsl:stylesheet>
属性的添加 以及sort排序的使用
<xsl:template match="/">
<bib>
<xsl:apply-templates select="//book[./publisher/text()='Addison-Wesley'][number(./attribute::year)>1991]">
<xsl:sort select="./title/text()"/>
</xsl:apply-templates>
</bib>
</xsl:template>
<xsl:template match="book">
<book>
<xsl:copy-of select="./attribute::year"/>
<xsl:copy-of select="./title"/>
</book>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="//book//*[contains(string(.), 'Suciu')and ends-with(local-name(.), 'or')]"/>
</xsl:template>
<xsl:template match="*">
<book>
<xsl:copy-of select="./ancestor::book/title"/>
<xsl:copy-of select="."/>
</book>
</xsl:template>
</xsl:stylesheet>
分享到:
相关推荐
Beginning.XSLT.and.XPath_Transforming.XML.Documents.and.Data
Beginning.XSLT.and.XPath_Transforming.XML.Documents.and.Data源码
4.19.5 指定文档缩进 161 4.19.6 指定媒体类型 161 4.20 xslt中的函数 162 4.21 数字格式化 162 4.22 查询和分组 164 4.23 处理多个输入文档 172 4.24 jaxp中的xslt api 175 4.24.1 转换器工厂 175 4.24.2 ...
编写book.xslt文档,要求在book.xml中打印出所有的section元素(包括属性)及其title子元素(并且保持section元素之间的嵌套关系);输出结果如下:
在文档内每一XML元素接受解析的前后触发元素事件。 任何元数据通常都由单独的事件交付。 在处理文档的DTD或Schema时产生DTD或Schema事件。 产生错误事件用来通知主机应用程序解析错误。
XML笔记+XSLT中文入门 XML笔记+XSLT中文入门XML笔记+XSLT中文入门 XML笔记+XSLT中文入门
在Coreldraw 工具栏鼠标点击 右键----菜单----自定义----工作区----导入工作区---选择要导入的工作区文件 窗口,浏览-选择“win10_x4菜单.xslt”文件,例如选择工作区“WIN10下_x4.x5.x6菜单文件”文件夹下的“win10...
Practical.Transformation.with.XSLT.and.XPath
Wrox.XSLT.Programmer's.Reference.Second.Edition
学习如何使用XMLSPY集成开发环境执行XPath查询、以及XSLT的数据转换。 2、理解XPath的类型系统和序列类型等相关概念,掌握XPath表达式语法和执行语义,能够熟练地、灵活地运用各种关系轴、判定谓词和XPath基本函数...
Que.Special.Edition.Using.XSLT.Jan.2002
1、学习如何使用XMLSPY集成开发环境执行XPath查询、以及XSLT的数据转换。 2、理解XPath的类型系统和序列类型等相关概念,掌握XPath表达式语法和执行语义,能够熟练地、灵活地运用各种关系轴、判定谓词和XPath基本...
testng-xslt-1.1.2.zip
Wrox.XSLT.2.0.and.XPath.2.0.Programmers.Reference.4th.Edition.May.2008.rar
第9章 XML编程... 323 9.1 XML简介及其特征... 323 9.1.1 XML 简介......9.6.2基于XSLT样式表的XML文档显示... 346 9.6.3基于数据岛的XML文档显示... 354 9.6.4 基于javascript的XML文档显示... 357
Welcome to XSLT and XPath, two members of the W3C XML family of standards. This book concentrates on using XSLT and XPath to solve problems that you are likely to encounter every day in writing XSLT ...
CoreDraw-X4X5X6-Win10菜单栏异常问题
本系列包含:XMLDOM对象方法手册.chm XmlSchema标准参考手册.chm XMLHTTP手册.CHM XML指南.chm XSLT标准参考手册.chm
Learning XSLT moves smoothly from the simple ... Learning XSLT also explains how the XML Path Language (XPath) is used by XSLT and provides a glimpse of what the future holds for XSLT 2.0 and XPath 2.0.
利用XSLT转换XML.pdf利用XSLT转换XML.pdf利用XSLT转换XML.pdf利用XSLT转换XML.pdf