[ 引文 1 XSLT 中 XPath 的作用 ]
In XSLT 1.0, XPath plays three crucial roles:
?First, it is used within templates for addressing into the document to extract data as it is being transformed.
Second, XPath syntax is used as a pattern language in the matching rules for templates.
Third, it is used to perform simple math and string manipulations via built-in XPath operators and functions.
XSLT 2.0 retains and strengthens this intimate connection with XPath 2.0 by drawing heavily on the new computational abilities of XPath 2.0.
[ 引文 2 當前節點的設置 ]
In XSLT, the context is set via:
·???????? a template match (<xsl:template match="x"> ... </xsl:template>)
·???????? xsl:for-each
·???????? xsl:apply-templates
xsl:template 與 xsl:apply-templates
前者相當于定義函數,后者相當于調用函數。
在
xsl:template
中,
match
屬性采用了
XPath
表達式,用來確定模板的適用對象。對
match
屬性的設置時,
//X
等價于
X
。
在
xsl:apply-templates
中,
select
屬性采用了
XPath
表達式,用來選擇要應用模板的節點。如果省略了
select
屬性,則表示當前節點的所有后代節點都應用模板,如果有些節點沒用通過
xsl:template
定義模板,則采用默認模板(注:
IE
中存在默認模板)。設置
select
屬性時,要從文檔根(
/
)開始的絕對路徑來選擇節點,但是
/
可以省略。此處和上面
match
屬性差別很大,注意區分。
注意:
1
)
<xsl:template match="/">
??
??
?
?<xsl:apply-templates select="/" />
</xsl:template>
上面語句會引起遞歸調用模板,死循環。
2
)如果在
XSL
文件中沒有對應根的模板,即沒有
<xsl:template match="/">
,則其他的模板都會被使用。但是,如果文件中存在對應根的模板,除非使用
<xsl:apply-templates select="
……
" />
,否則其他的模板都不會被使用。
這是因為,如果沒有對應根的模板,
IE
會采用默認的對應根的模板,而這個默認的模板中使用了
<xsl:apply-templates select="
……
" />
。這類似于
Java
中默認構造函數的重寫,如果我們沒有定義,就使用默認的。