您可以在 XSLT 中模拟布尔标志吗?

R.C*_*.C. 1 xslt

我想在 xslt 脚本中模拟一个标志。这个想法是为模板 foo 设置一个标志(或计数器变量,或任何东西),以便可以从模板 bar 访问它。Bar 不是从 foo 调用的,而是从公共父模板调用的(否则我会向它传递一个参数)。结构是这样的:

<xsl:template match="bla">
  <xsl:apply-templates select="foo"/> <!-- depending on the contents of foo... -->
  <xsl:apply-templates select="bar"/> <!-- ... different things should happen in bar -->
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

任何技巧都非常感激。

Lor*_*nVS 5

不是真的……至少不是从你试图这样做的意义上来说。XSLT 中的变量是不可变的,一旦为它们分配了值,就无法更改它们,因此尝试多次调用 foo 来更改标志的值是行不通的。您可以尝试几种模式,它们可能会完成您想要做的事情,例如:

<xsl:variable name="myFlag"><xsl:apply-templates select="foo" /></xsl:variable>

<xsl:template match="bla">
      <xsl:apply-templates select="bar" /> <!-- Can use the value of $myFlag --.
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

如果构建模板 foo 来返回标志的值,那么这将起作用,但是,如果标志的值要随着时间的推移而改变,那么实现这一点的唯一真正方法是将 foo 的调用合并到 bar 模板中。

<xsl:template match="bla">
    <xsl:apply-templates select="bar"> />
</xsl:template>

<xsl:template match="bar">
   <xsl:variable name="flag"><xsl:apply-templates name="foo" /></xsl:variable>

   <xsl:if test="$flag=1">

   </xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)