| <xsl:apply-imports> | |
| 上書きされたテンプレートを現在のノードに適用できます。これは Java の super() メソッドに相当します。 | |
| カテゴリ | |
|
命令 |
|
| 必須の属性 | |
|
なし。 |
|
| 省略可能な属性 | |
|
なし。 |
|
| コンテンツ | |
|
なし。<xsl:apply-imports> は空の要素です。 |
|
| 指定先 | |
|
<xsl:apply-imports> はテンプレート内に指定します。 |
|
| 定義先 | |
|
XSLT 5.6 節「Overriding Template Rules」 |
|
| 例 | |
|
<xsl:apply-imports> を示すために使用する短い XML ファイルを次に示します。
<?xml version="1.0"?>
<test>
<p>This is a test XML document used by several
of our sample stylesheets.</p>
<question>
<text>When completed, the Eiffel Tower was the
tallest building in the world.</text>
<true correct="yes">You're correct! The Eiffel
Tower was the world's tallest building until 1930.</true>
<false>No, the Eiffel Tower was the world's tallest
building for over 30 years.</false>
</question>
<question>
<text>New York's Empire State Building knocked the
Eiffel Tower from its pedestal.</text>
<true>No, that's not correct.</true>
<false correct="yes">Correct! New York's Chrysler
Building, completed in 1930, became the world's tallest.</false>
</question>
</test>
読み込むスタイルシートは次のとおりです。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//text|//true|//false">
<p>
<xsl:apply-templates select="."/>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="text">
<xsl:text>True or False: </xsl:text><xsl:value-of select="."/>
</xsl:template>
<xsl:template match="true|false">
<b><xsl:value-of select="name()"/>:</b>
<br/>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
図 A-1 に示すように、このテンプレートは、<true> および <false> 要素に基本的なフォーマットを提供します。 基本的なフォーマットで生成されたドキュメント このスタイルシートを使用して、他のスタイルシートを読み込む <xsl:apply-imports> を示します。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="imported.xsl"/>
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>A Brief Test</title>
<style>
<xsl:comment>
p.question {font-size: 125%; font-weight: bold}
p.right {color: green}
p.wrong {color: red}
</xsl:comment>
</style>
</head>
<body>
<h1>A Brief Test</h1>
<xsl:for-each select="//question">
<table border="1">
<xsl:apply-templates select="text"/>
<xsl:apply-templates select="true|false"/>
</table>
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="text">
<tr bgcolor="lightslategray">
<td>
<p class="question">
<xsl:apply-imports/>
</p>
</td>
</tr>
</xsl:template>
<xsl:template match="true|false">
<tr>
<td>
<xsl:choose>
<xsl:when test="@correct='yes'">
<p class="right">
<xsl:apply-imports/>
</p>
</xsl:when>
<xsl:otherwise>
<p class="wrong">
<xsl:apply-imports/>
</p>
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
<xsl:apply-imports> を使用すると、読み込んだテンプレートの動作を強化できます。新しいスタイルシートでは、次のドキュメントが生成されます。
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>A Brief Test</title>
<style>
<!--
p.question {font-size: 125%; font-weight: bold}
p.right {color: green}
p.wrong {color: red}
-->
</style>
</head>
<body>
<h1>A Brief Test</h1>
<table border="1">
<tr bgcolor="lightslategray">
<td>
<p class="question">True or False: When completed, the Eiffel
Tower was the tallest building in the world.</p>
</td>
</tr>
<tr>
<td>
<p class="right">
<b>true:</b>
<br>You're correct! The Eiffel Tower was the world's tallest
building until 1930.</p>
</td>
</tr>
<tr>
<td>
<p class="wrong">
<b>false:</b>
<br>No, the Eiffel Tower was the world's tallest building for
over 30 years.</p>
</td>
</tr>
</table>
<br>
<table border="1">
<tr bgcolor="lightslategray">
<td>
<p class="question">True or False: New York's Empire State Building
knocked the Eiffel Tower from its pedestal.</p>
</td>
</tr>
<tr>
<td>
<p class="wrong">
<b>true:</b>
<br>No, that's not correct.</p>
</td>
</tr>
<tr>
<td>
<p class="right">
<b>false:</b>
<br>Correct! New York's Chrysler Building, completed in 1930,
became the world's tallest.</p>
</td>
</tr>
</table>
<br>
</body>
</html>
このスタイルシートの表示を図 A-2 に示します。 <xsl:apply-imports> で生成されたドキュメント |
|