<xsl:comment>  
出力ドキュメントでコメントを作成できます。コメントは、法律上の注意事項、免責条項、出力ドキュメントを作成した日時に関する情報などを追加するために使用されることがあります。<xsl:comment> 要素は、HTML ドキュメントで CSS 定義や JavaScript コードを生成する場合にも役立ちます。
 
カテゴリ

命令

 
必須の属性

なし。

 
省略可能な属性

なし。

 
コンテンツ

XSLT テンプレート。

 
指定先

<xsl:comment> はテンプレート内に指定します。

 
定義先

XSLT 7.4 節「Creating Comments」

 

HTML ドキュメントで CSS スタイルを定義するためにコメントを生成するスタイルシートを次に示します。

<?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>
      <head>
        <title>XSLT and CSS Demo</title>
        <style>
          <xsl:comment> 
            p.big      {font-size: 125%; font-weight: bold} 
            p.green    {color: green; font-weight: bold}
            p.red      {color: red; font-style: italic}
          </xsl:comment>
        </style>
      </head>
      <body>
        <xsl:apply-templates select="list/title"/>
        <xsl:apply-templates select="list/listitem"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="title">
    <p class="big"><xsl:value-of select="."/></p>
  </xsl:template>

  <xsl:template match="listitem">
    <xsl:choose>
      <xsl:when test="position() mod 2">
        <p class="green"><xsl:value-of select="."/></p>
      </xsl:when>
      <xsl:otherwise>
        <p class="red"><xsl:value-of select="."/></p>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>

このスタイルシートは、HTML コメント内に 3 つの CSS スタイルを作成します。このドキュメントにスタイルシートを適用します。

<?xml version="1.0"?>
<list xml:lang="en">
  <title>Albums I've bought recently:</title>
  <listitem>The Sacred Art of Dub</listitem>
  <listitem>Only the Poor Man Feel It</listitem>
  <listitem>Excitable Boy</listitem>
  <listitem xml:lang="sw">Aki Special</listitem>
  <listitem xml:lang="en-gb">Combat Rock</listitem>
  <listitem xml:lang="zu">Talking Timbuktu</listitem>
  <listitem xml:lang="jz">The Birth of the Cool</listitem>
</list>

このスタイルシートは、1 つの CSS スタイルを <title> 要素に適用し、<listitem> に対して 2 つの CSS スタイルを変更します。生成された HTML は次のとおりです。

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XSLT and CSS Demo</title>
<style>
<!-- 
            p.big      {font-size: 125%; font-weight: bold} 
            p.green    {color: green; font-weight: bold}
            p.red      {color: red; font-style: italic}
          -->
</style>
</head>
<body>
<p class="big">Albums I've bought recently:</p>
<p class="green">The Sacred Art of Dub</p>
<p class="red">Only the Poor Man Feel It</p>
<p class="green">Excitable Boy</p>
<p class="red">Aki Special</p>
<p class="green">Combat Rock</p>
<p class="red">Talking Timbuktu</p>
<p class="green">The Birth of the Cool</p>
</body>
</html>

このドキュメントの表示を図 A-6 に示します。

生成されたコメントノードが含まれたドキュメント