http://blog.csdn.net/xiaoqiangpku725/article/details/4038282
有兩種基本方法提取源元素中的內容。 stylesheet 使用 xsl:value-of 來獲得,此時 已經處理得元素的內容不會再被進一步處理。 stylesheet 中的 xsl:apply-templates 指令卻不同,解析器將按照模板的定義逐個繼續處理。
也就是說:
1. xsl:apply-templates 放在xsl:template中使用,可以理解爲子函數.
2. 多個xsl:template同時使用時,已經MATCH元素得子元素將不被處理.
如果還不能理解,看下面得例子:
XML源碼:
<source>
<company>microsoft</company>
<employee>
<firstName>lily</firstName>
<surname>hu</surname>
</employee>
<telephone>
<telephone1>1</telephone1>
<telephone1>2</telephone1>
</telephone>
</source>
XSLT stylesheet 1:
//使用 xsl:value-of 來獲得,此時元素的內容不會再被進一步處理。
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="company">
<b>
<xsl:value-of select="."/>
</b>
</xsl:template>
<xsl:template match="employee">
<b>
<xsl:value-of select="."/>
</b>
</xsl:template>
<xsl:template match="surname"> //surname是employee得子元素,故不作處理;
<i>
<xsl:value-of select="."/>
</i>
</xsl:template>
<xsl:template match="telephone1">
<i>
<xsl:value-of select="."/>
</i>
</xsl:template>
</xsl:stylesheet>
輸出:
microsoft
<b>
lily
hu
</b>
<i>
1
</i>
XSLT stylesheet 2
//使用apply-templates 指令,解析器將按照模板的定義逐個繼續處理
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="employee">
<b>
<xsl:apply-templates select="firstName"/>
</b>
<b>
<xsl:apply-templates select="surname"/>
</b>
</xsl:template>
<xsl:template match="surname">
<i>
<xsl:value-of select="."/>
</i>
</xsl:template>
</xsl:stylesheet>
輸出:
<b>lily</b>
<b>
<i>hu</i>
</b>
wwater 發表於 2006-11-16 17:44:00
比如說,有這樣一個文檔結構
<Root>
<Row>
<name>1</name>
<age>12</age>
</Row>
<Row>
<name>2</name>
<age>13</age>
</Row>
</Root>
你寫了一個模板匹配Row
<xsl:template match="Row">
<xsl:apply-templates/>
</xsl:template>
<xsl:apply-templates/>會自行匹配Row下的所有Element和Attribute
如果你沒有爲name和age寫模板,缺省顯示它的文本信息。如果你定義了以下兩個模板
<xsl:template match="name">
姓名:<xsl:value-of select="text()"/>
</xsl:template>
<xsl:template match="age">
年齡:<xsl:value-of select="text()"/>
</xsl:template>
它就會一一進行匹配。