RTFはわかりにくい

W3Cの勧告文にはわかりやすい例がないのだが、変数を作る場合select属性を使った場合とテンプレートとして書いた場合では結果が違う。

例えば、document()関数を使って

<xsl:param name="foo" select="document('hoge.xml')"/>

と書くと$fooにはノード集合が格納される。

If the variable-binding element has a select attribute, then the value of the attribute must be an expression and the value of the variable is the object that results from evaluating the xpression. In this case, the content must be empty.

上記のようにselect属性が有る場合は式を評価した結果のオブジェクト(この場合はノード集合)が格納される。

xsl:paramもしくはxml:variableの内容としてテンプレートを書いた場合は結果ツリーフラグメントが格納される。

<xsl:param name="bar">
    <xsl:copy-of select="document('hoge.xml')"/>
</xsl:param>

それぞれの変数はデータ型が違うのでその後の処理で使う場合は注意が必要になる。

<!-- これは問題ない -->
<xsl:apply-templates select="$foo"/>

<!-- これはエラー -->
<xsl:apply-templates select="$bar"/>