- PR -

[XSLT]の挙動について

1
投稿者投稿内容
nari
会議室デビュー日: 2008/05/29
投稿数: 7
投稿日時: 2009-02-20 10:40
今、XSLTの<xsl:apply-imports>について勉強中なのですが、
<xsl:apply-imports>の挙動がよくわかりません。例えば

・XMLファイル
<?xml version="1.0" encoding="Shift_JIS"?>
<books>
<book>
<title><sub>サブ</sub>タイトル</title>
</book>
</books>

・XSLTファイル(xsltA.xsl)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="xsltB.xsl"/>
<xsl:output method="xml"/>
<xsl:template match="/">
<a><xsl:apply-templates/></a>
</xsl:template>
<xsl:template match="title">
<xsl:apply-imports/>
</xsl:template>
<xsl:template match="sub">
<b><xsl:value-of select="."/></b>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>

・XSLTファイル(xsltB.xsl)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="sub">
<c><xsl:value-of select="."/></c>
</xsl:template>
</xsl:stylesheet>


のような定義があった場合、sub要素のテンプレートに競合が起こるため
xsltB.xslのsubテンプレートは適用されません。
但し、<xsl:apply-imports>を使うと適用されなかった
xsltB.xslのsubテンプレートが適用されるという認識で処理しましたら
やはりxsltB.xslは適用されませんでした。
その結果、変換結果は

<a><b>サブ</b></a>

となってしまうのですが、これはなぜでしょうか?
私が勘違いしている部分はどこなのかを教えて頂けないでしょうか?

以上、よろしくお願いします。
aiaru
会議室デビュー日: 2009/02/10
投稿数: 7
投稿日時: 2009-02-25 18:17
だいぶ日にちがたっていますが、参考まで。


<template match="title">
  <xsl:apply-imports />
  <!--
    #↑ここで呼び出したら、現在のノード(current node)をインポートされた
    テンプレート規則だけを使用して処理する。
    つまり、処理されるノードは match="title" のノード
  -->
</template>


と言うことかと。

<xsl:template match="sub">
<b><xsl:apply-imports /></b>
</xsl:template>

という使い方のための機能かと思います。


[参考URL]
http://www.w3.org/TR/xslt#element-apply-imports
#5.6 Overriding Template Rules

→ xsl:apply-imports processes the current node using only template rules that were imported into the stylesheet element containing the current template rule;
nari
会議室デビュー日: 2008/05/29
投稿数: 7
投稿日時: 2009-02-26 08:51
なるほど、カレントノードに適用されるtemplateが競合している場合に
apply-importsは意味がある、ということですね。

W3Cドキュメントへのリンクも助かります。
ありがとうございました!
1

スキルアップ/キャリアアップ(JOB@IT)