Archive for May, 2008

4月影片推荐 1

1. 《Hitman》

觉得比较值得看的就只有《Hitman》(杀手47),杀人酷的决不失手,干净利落;解决问题冷静有条理;对女人则铁汉柔情。很喜欢看这类型的片子,包括最早最经典的《The Professional》(这个杀手不太冷),还有比较近的整部片都很紧张的伯恩三部曲。《Hitman》是由游戏改编而来的,情节设计得很紧凑,不知是不是和游戏里面的情节一样,不过现在是没时间玩游戏了。

2. 《10,000 BC》

如果还有时间的话,不妨看看《10,000 BC》(史前一万年)。主要是一万年前人们生活的场景吸引了我,还有制作得不错的怪兽。若是论情节,也就只是一部英雄救美女的片子。不过我觉得这是值得的,因为美女很美,一点都不像历史课本上公元一万年前人们该有的脸庞。顺便还学了一句古语“Yahala”,就是“We must bring them down”的意思。说不定可以作为下个项目的代号。

XSLT 1.0:用call-template还是apply-templates? 0

在五一放假前和Mike讨论到这个问题,之前我用了比较多的call-template,后来说着说着,突然发现apply-templates在大部分的场景下比call-template好用,也更安全一些。

比如,apply-templates可以通过select属性来选择具体要匹配的node-set,如果不存在这样的node-set,就不进行转换,这就比call-template安全了一些,因为后者是直接调用,不管存不存在某些节点;其次,apply-templates和call-template一样,可以通过<xsl:param/>接受参数;此外,当我们定义多个template时,这些templates不可以有重复的name,但可以有重复的match,重复的template通过priority来确定该用哪个template来进行转换,如果多个template有相同的priority的话,则选择最后出现的那个。

似乎apply-templates功能比call-template强。就目前我所用到的普通转换来说,用apply-templates是比较安全的,并且可读性也会高一些。但是在XSLT 1.0中,如果想要实现像XSLT 2.0中function一样的功能的话,用call-template就比较方便了,比如递归调用(Recursive function)。下面是一个例子:

<xsl:template name=”longest-speech” as=”element(SPEECH)?”>
<xsl:param name=”list” as=”element(SPEECH)*”/>
<xsl:choose>
<xsl:when test=”$list”>
<xsl:variable name=”first” select=”count($list[1]/LINE)” as=”xs:integer”/>
<xsl:variable name=”longest-of-rest” as=”element(SPEECH)?”>
<xsl:call-template name=”longest-speech”>
<xsl:with-param name=”list” select=”$list[position()!=1]“/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test=”$first gt count($longest-of-rest/LINE)”>
<xsl:value-of select=”$list[1]“/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=”$longest-of-rest”/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match=”/”>
<longest-speech>
<xsl:call-template name=”longest-speech”>
<xsl:with-param name=”list” select=”//SPEECH”/>
</xsl:call-template>
</longest-speech>
</xsl:template>

待转换的xml是:

<?xml version=”1.0″?>
<SCENE><TITLE>SCENE I. Venice. A street.</TITLE>
<STAGEDIR>Enter RODERIGO and IAGO</STAGEDIR>

<SPEECH>
<SPEAKER>RODERIGO</SPEAKER>
<LINE>Tush! never tell me; I take it much unkindly</LINE>
<LINE>That thou, Iago, who hast had my purse</LINE>
<LINE>As if the strings were thine, shouldst know of this.</LINE>
</SPEECH>

<SPEECH>
<SPEAKER>IAGO</SPEAKER>
<LINE>’Sblood, but you will not hear me:</LINE>
<LINE>If ever I did dream of such a matter, Abhor me.</LINE>
</SPEECH>
etc.
</SCENE>

在关于使用call-template和apply-templates的性能差别上,Sam Judson (Wrox 技术编辑)有言:

In terms of raw performance xsl:call-template is likely to be faster, as you are calling a specific named template, rather than telling the XSLT processor to pick the template which best matches.

There are certainly things you can do with xsl:call-template that you can’t do with xsl:apply-templates, such as recursive templates which are very powerful.

xsl:apply-templates is however the more flexible and extensible, due to its combined use of the match patterns, modes and priorities.

我觉得概括的不错,言简意赅,就以此作为结尾吧。