在 xslt 中添加一个或递增变量 [英] Adding one or incrementing variable in xslt

查看:21
本文介绍了在 xslt 中添加一个或递增变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 Counter 值根据条件递增.但是我无法增加值或将 1 添加到全局变量.我正在使用以下代码

I want to make a Counter value getting incremented based on a condition. But i was not able to increment a value or add 1 to a global variable. I was using the following code

<xsl:variable name="count">0</xsl:variable>
<xsl:variable name="incre">1</xsl:variable>    

<xsl:template match="/">
  <xsl:if test="$video1 != ''">
    <xsl:variable name="count" select="number($count)+number($incrementor)"/>
    <!-- display video1 -->
  </xsl:if>
  <xsl:if test="$video2 != ''">
    <xsl:variable name="count" select="number($count)+number($incrementor)"/>
    <!-- display video2 -->
  </xsl:if>
  <xsl:if test="$video3 != ''">
    <xsl:if test="$count<2">
      <xsl:variable name="count" select="number($count)+number($incrementor)"/>
      <!-- display video3 -->
    </xs:if>
  </xsl:if>
  <xsl:if test="$video4 != ''">
    <xsl:if test="$count<2">
      <xsl:variable name="count" select="number($count)+number($incrementor)"/>
      <!-- display video4 -->
    </xs:if>
  </xsl:if>
</template>

如何增加 count 的值或给它加 1.提前致谢.

How to increment the value of count or add 1 to it. Thanks in advance.

推荐答案

您的问题源于在尝试使用函数式语言时像程序程序员一样思考.这是一个糟糕的组合;与工具配合使用会更好地发挥作用,而不是对抗它们.

Your problem arises from thinking like a procedural programmer when trying to use a functional language. It's a bad mix; tools work a lot better when you work with them and not against them.

不要试图遍历文档并增加变量以保持计数.用声明性的术语描述你想要什么.

Don't try to walk through the document and increment variables to keep count of things. Describe what you want in declarative terms.

在这里,您希望显示文档中的前两个非空 元素,而您希望其他元素(所有空的 > 元素和第三个及之后的非空 元素)以静默方式传递.(当然,这是我编造的:因为您没有显示您的 XML,所以我实际上不知道您真正想要什么,在 XML 术语中.)

Here, you want the first two non-empty <video> elements in the document to display, and you want the others (all the empty <video> elements and the third and later non-empty <video> elements) to be passed over in silence. (I'm making this up, of course: since you don't show your XML, I don't actually know what you really want, in XML terms.)

所以你想要一个模板来抑制空的 元素:

So you want a template to suppress empty <video> elements:

<xsl:template match="video[not(node())]"/>

并且您想要一个模板来抑制文档中前面有两个或多个非空 元素的任何 元素:

And you want a template to suppress any <video> element preceded in the document by two or more non-empty <video> elements:

<xsl:template 
  match="video[count(preceding::video
                     [* or normalized-space(.)]
                    ) 
               > 1]"/>

并且您想要一个模板来显示前两个非空的 元素:

And you want a template to display the first two non-empty <video> elements:

<xsl:template 
  match="video[* or normalized-space(.)]
              [count(preceding::video
                     [* or normalized-space(.)]
                    ) 
               &lt; 2]">

  <xsl:message>Displaying video ... </xsl:message>
  <!--* ... whatever else is needed to display the video ... *-->

<xsl:template>

这篇关于在 xslt 中添加一个或递增变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆