1

次のようなコードがあります。

<xsl:choose>
   <xsl:when test="some_test">   
      <xsl:value-of select="Something" />
      You are:
      <xsl:variable name="age">12</xsl:variable>
      years
   </xsl:when>
</xsl:choose>

私の問題は、選択の外で変数 $age を使用したいということです。それ、どうやったら出来るの?

同様の問題は、XSLT ファイルにいくつかのテンプレートがあり、そのうちの 1 つがメイン テンプレートであることです。これです:

<xsl:template match="/">
</xsl:template>

このテンプレート内で、他のいくつかのテンプレートを呼び出します。ここでも、他のテンプレートのいくつかの変数を使用したいと思います。

たとえば、次のコードがあるとします。

<xsl:template match="/">
   <xsl:call-template name="search">
   </xsl:call-template>
</xsl:template>

<xsl:template name="search">
   <xsl:variable name="searchVar">Something...</xsl:variable>
</xsl:template>

次に、メイン テンプレート内で $searchVar を使用したいと思います。

それは私が思うのと同じ問題のようなものですが、私はこれを理解できないようです。

ちなみに、CMSとしてUmbracoを実行しています:)

どなたかお答えいただけると幸いです。

ありがとう - キム

4

2 に答える 2

2

#1:変数は親要素内でのみ有効です。つまり、ロジックを変数の「周り」ではなく、変数に配置する必要があります。

<xsl:variable name="var">
  <xsl:choose>
    <xsl:when test="some_test">
      <xsl:text>HEY!</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>SEE YA!</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

#2へ:パラメータを使用して値をテンプレートに転送します。

<xsl:template match="/">
  <xsl:variable name="var" select="'something'" />

  <!-- params work for named templates.. -->
  <xsl:call-template name="search">
    <xsl:with-param name="p" select="$var" />
  </xsl:call-template>

  <!-- ...and for normal templates as well -->
  <xsl:apply-templates select="xpath/to/nodes">
    <xsl:with-param name="p" select="$var" />
  </xsl:apply-templates>
</xsl:template>

<!-- named template -->
<xsl:template name="search">
  <xsl:param name="p" />

  <!-- does stuff with $p -->
</xsl:template>

<-- normal template -->
<xsl:template match="nodes">
  <xsl:param name="p" />

  <!-- does stuff with $p -->
</xsl:template>

値を呼び出し元のテンプレートに戻すには、上記を組み合わせます。

<xsl:template match="/">
  <xsl:variable name="var">
    <xsl:call-template name="age">
      <xsl:with-param name="num" select="28" />
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="$var" />
</xsl:template>

<xsl:template name="age">
  <xsl:param name="num" />

  <xsl:value-of select="concat('You are ', $num, ' years yold!')" />
</xsl:template>
于 2009-09-11T11:40:44.997 に答える
0

xsl:paramを見てください。

編集:テストされていませんが、動作する可能性があります:

<xsl:param name="var">
  <xsl:choose>
   <xsl:when test="some_test">   
     <xsl:value-of select="string('HEY!')" />
   </xsl:when>
  </xsl:choose>
</xsl:param>
于 2009-09-11T10:10:27.210 に答える