XSLTで、chooseの内側、chooseの外側で定義された変数を再利用します

StackOverflow https://stackoverflow.com/questions/1410069

  •  05-07-2019
  •  | 
  •  

質問

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

<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>

私の問題は、choice以外で変数$ 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を実行しています:)

あなたの何人かが答えを持っていることを望みます。

ありがとう -キム

役に立ちましたか?

解決

#1:変数は、親要素内でのみ有効です。つまり、ロジックを&quot; around&quot;ではなく、変数の inside に配置する必要があります。それ:

<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>

他のヒント

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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top