<xsl:import> | |
他の XSLT スタイルシートに含まれたテンプレートを読み込むことができます。<xsl:include> とは異なり、<xsl:import> を使用して読み込んだすべてのテンプレートは、スタイルシートに含まれるテンプレートよりも優先順位が低くなります。また <xsl:include> と <xsl:import> のその他の相違点として、<xsl:include> はスタイルシートの任意の場所に指定できますが、<xsl:import> はスタイルシートの先頭にしか指定できないことが挙げられます。 | |
カテゴリ | |
トップレベル要素 |
|
必須の属性 | |
|
|
省略可能な属性 | |
なし。 |
|
コンテンツ | |
なし。<xsl:import> は空の要素です。 |
|
指定先 | |
<xsl:import> は、トップレベル要素であり、<xsl:stylesheet> の子としてのみ使用できます。 |
|
定義先 | |
XSLT 2.6.2 節「Stylesheet Import」 |
|
例 | |
次の簡単なスタイルシートを読み込みます。 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:apply-templates select="list/title"/> <xsl:apply-templates select="list/listitem"/> </xsl:template> <xsl:template match="title"> <xsl:value-of select="."/> <xsl:text>: </xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> </xsl:template> <xsl:template match="listitem"> <xsl:text>HERE IS LISTITEM NUMBER </xsl:text> <xsl:value-of select="position()"/> <xsl:text>: </xsl:text> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:template> </xsl:stylesheet> このスタイルシートと、このスタイルシートを読み込んだスタイルシートの両方を次の XML ドキュメントに使用してテストします。 <?xml version="1.0"?> <list> <title>A few of my favorite albums</title> <listitem>A Love Supreme</listitem> <listitem>Beat Crazy</listitem> <listitem>Here Come the Warm Jets</listitem> <listitem>Kind of Blue</listitem> <listitem>London Calling</listitem> <listitem>Remain in Light</listitem> <listitem>The Joshua Tree</listitem> <listitem>The Indestructible Beat of Soweto</listitem> </list> このスタイルシートを使用して XML ソースドキュメントを処理した結果は次のとおりです。 A few of my favorite albums: HERE IS LISTITEM NUMBER 1: A Love Supreme HERE IS LISTITEM NUMBER 2: Beat Crazy HERE IS LISTITEM NUMBER 3: Here Come the Warm Jets HERE IS LISTITEM NUMBER 4: Kind of Blue HERE IS LISTITEM NUMBER 5: London Calling HERE IS LISTITEM NUMBER 6: Remain in Light HERE IS LISTITEM NUMBER 7: The Joshua Tree HERE IS LISTITEM NUMBER 8: The Indestructible Beat of Soweto <xsl:import> を使用した別のスタイルシートを次に示します。 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="listitem.xsl"/> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:apply-templates select="list/title"/> <xsl:apply-templates select="list/listitem"/> </xsl:template> <xsl:template match="listitem"> <xsl:value-of select="position()"/> <xsl:text>. </xsl:text> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:template> </xsl:stylesheet> この 2 つ目のスタイルシートにより作成された結果は次のとおりです。 A few of my favorite albums: 1. A Love Supreme 2. Beat Crazy 3. Here Come the Warm Jets 4. Kind of Blue 5. London Calling 6. Remain in Light 7. The Joshua Tree 8. The Indestructible Beat of Soweto 両方のスタイルシートに、match="listitem" を使用してテンプレートが指定されています。読み込まれたスタイルシート内のテンプレートは優先順位が低いため、使用されません。また、match="title" を使用したテンプレートは読み込まれたスタイルシートにのみ指定されているので、読み込まれたテンプレートは <title> 要素に対して使用されます。 |