我剛開始學習XSL(T),我想知道apply-templates
是如何工作的?我不明白遞歸應用模板的一部分,因爲它是寫在我的書中。apply-templates如何工作?
我理解XSL的XPath的部分(T)等,但沒有什麼apply-templates
在做什麼,爲什麼我把它寫多次。
我剛開始學習XSL(T),我想知道apply-templates
是如何工作的?我不明白遞歸應用模板的一部分,因爲它是寫在我的書中。apply-templates如何工作?
我理解XSL的XPath的部分(T)等,但沒有什麼apply-templates
在做什麼,爲什麼我把它寫多次。
如果您已閱讀過有關書中的應用模板但尚未理解的內容,則不清楚這裏的幾句話會對您有所幫助。也許你需要一本不同的書:不同的教程風格吸引不同的人。或者,也許一個在線教程,如http://vimeo.com/15234803將得到的想法。
模板機制的實質是有兩方參與。 xsl:apply-templates指令選擇一些節點進行處理,模板規則(它們之間)決定處理的內容。這導致了非常鬆散的耦合和關注的很大分離;它更像是面向對象的消息/方法發送,但更加靈活。
您使用<xsl:apply-templates>
來調用您已定義的<xsl:template>
:s。
<xsl:apply-templates>
爲集合中的每個節點調用匹配模板。
您可以通過在apply-templates
上指定select
屬性來控制處理順序。
見這個例子從w3schools:
<?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>
第一apply-templates
遇到命名"cd"
元素每次調用cd
模板。
的cd
模板,依次調用title
和artist
模板來處理<cd>
的子元素。
title
在artist
之前處理。請注意,源XML中的artist
和title
元素的順序沒有區別。
您可以認爲apply-templates
與過程語言中的子程序調用相似。
如果您瞭解完成模板規則!即使這並不容易,他們總是會給人驚喜。 Read the specs。
請發佈一些其他的XSLT視頻教程link.Bcoz上述教程給出了apply-templates的清晰思路,同時我也需要知道使用mode ='parent'的目的... – 2013-05-03 05:46:07
讓我推薦我的書:來自Wrox Press的XSLT 2.0程序員參考。一旦你進入像命名模式這樣的東西,你需要一本體面的參考書。 – 2013-05-03 07:14:56