XSLT.Cookbook2 学习笔记1--XPath

Posted on 2006-06-02 16:42 飞马 阅读(186) 评论(0)  编辑  收藏 所属分类: XML

[ 引文 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 中默认构造函数的重写,如果我们没有定义,就使用默认的。


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 1, comments - 0, trackbacks - 0, articles - 5

Copyright © 飞马