在 XSLT 中增加一个值 [英] Increment a value in XSLT

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

问题描述

我对 xlst 相当陌生,并且不知道是否有任何方法可以存储值并在以后更改它,例如在循环中递增变量.

I'm reasonably new to xlst and am confused as to whether there is any way to store a value and change it later, for example incrementing a variable in a loop.

我有点困惑,因为在设置 a 后无法更改它的值对我来说没有意义,使其更像是一个常量.

I'm a bit baffled by not being able to change the value of a after it's set doesn't make sense to me, making it more of a constant.

例如我想做这样的事情:

For example I want to do something like this:

<xsl:variable name="i" select="0" />
<xsl:for-each select="data/posts/entry">
    <xsl:variable name="i" select="$i + 1" />
    <!-- DO SOMETHING -->
</xsl:for-each>

如果有人可以启发我是否有其他方法可以做到这一点
谢谢

If anyone can enlighten me on whether there is an alternative way to do this
Thanks

推荐答案

XSLT 是一种函数式语言,这意味着XSLT 中的变量是不可变的,并且一旦它们已经被定义,它们的值不能改变.

XSLT is a functional language and among other things this means that variables in XSLT are immutable and once they have been defined their value cannot be changed.

这里是如何在 XSLT 中实现相同的效果:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
   <posts>
    <xsl:for-each select="data/posts/entry">
        <xsl:variable name="i" select="position()" />
        <xsl:copy>
         <xsl:value-of select="concat('$i = ', $i)"/>
        </xsl:copy>
    </xsl:for-each>
   </posts>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<data>
 <posts>
  <entry/>
  <entry/>
  <entry/>
  <entry/>
  <entry/>
 </posts>
</data>

结果是:

<posts>
    <entry>$i = 1</entry>
    <entry>$i = 2</entry>
    <entry>$i = 3</entry>
    <entry>$i = 4</entry>
    <entry>$i = 5</entry>
</posts>

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

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