<xsl:preserve-space>  
空白スペースを保持するソースドキュメントの要素を定義します。
 
カテゴリ

トップレベル要素

 
必須の属性
elements
この属性は、空白スペースを保持する要素を定義します。複数の要素を定義する必要がある場合は、要素名を 1 つまたは複数の空白スペース文字で区切ります。

 
省略可能な属性

なし。

 
コンテンツ

なし。<xsl:preserve-space> は空の要素です。

 
指定先

<preserve-space> はトップレベル要素であり、<xsl:stylesheet> の子としてのみ使用できます。

 
定義先

XSLT 3.4 節「空白スペースの削除」

 

<preserve-space> が次のスタイルシートでどのように動作するかを示します。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>
  <xsl:preserve-space elements="listing"/>

  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:value-of select="/code-sample/title"/>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="/code-sample/listing">
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

このスタイルシートを使用して次のドキュメントを処理します。

<?xml version="1.0"?>
<code-sample>
  <title>Conditional variable initialization</title>
  <listing>
  <type>int</type> <variable>y</variable> = <constant>23</constant>;
  <type>int</type> <variable>x</variable>;
    <keyword>if</keyword> (<variable>y</variable> > <constant>10</constant>)
    <variable>x</variable> = <constant>5</constant>;
  <keyword>else</keyword> 
    <keyword>if</keyword> (<variable>y</variable> > <constant>5</constant>)
      <variable>x</variable> = <constant>3</constant>;
  <keyword>else</keyword>
    <variable>x</variable> = <constant>1</constant>;
  </listing>
</code-sample>
      

このドキュメントをスタイルシートで処理すると、次の結果が得られます。


Conditional variable initialization

  int y = 23;
  int x;
    if (y > 10)
    x = 5;
  else
    if (y > 5)
      x = 3;
  else
    x = 1;

この例を <strip-space> 要素の例と比較してください。