`
阿尔萨斯
  • 浏览: 4169380 次
社区版块
存档分类
最新评论

XML学习笔记 5. XSLT

 
阅读更多

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>




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics