循环使用临时值在xsl中设置一组元素 [英] Loop over a set of elements in xsl with a temporary value

查看:50
本文介绍了循环使用临时值在xsl中设置一组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XSLT 1.0

I am using XSLT 1.0

假设我有一个包含某些"num"个元素的xml,这些元素可以在XML文档中的任何地方,例如:-

Suppose I have an xml that has certain "num" elements that could be anywhere in an XML document like this :-

<elem1>
  <num>24</num>
</elem1>
<elem2>
  <description>
    <num>18</num>
  </description>
</elem2>
<elem3>
  <elem1>
    <num>36</num>
  </elem1>
</elem3>

我想将其转换为:-

<elem1>
  <gcd multiple="4">6</gcd>
</elem1>
<elem2>
  <description>
    <gcd multiple="3">6</gcd>
  </description>
</elem2>
<elem3>
  <elem1>
    <gcd multiple="6">6</gcd>
  </elem1>
</elem3>

现在我有一个可以计算两个元素的gcd的模板:-

Now I have a template that can compute gcd of two elements like this :-

<xsl:template name="gcd">
  <xsl:param name="x"/>
  <xsl:param name="y"/>
  <!-- snipping code to compute gcd that goes here -->
</xsl:template>

鉴于此模板是gcd的,我想我将不得不对num元素进行诸如此类的循环:-

Given this template for a gcd, I'm guessing that I will have to do something like loop over the num elements like this :-

<xsl:variable name="global_gcd">
  <xsl:for-each select="//ns0:num">
    <!-- something probably goes here --> 
    <!-- say a temporary value x=0 initially -->
    <!-- and then x = gcd (x, value of current node) -->
  </xsl:for-each>
</xsl:variable>

然后,据称我可以在模板中将该变量用于"num",以生成所需的"gcd"元素以及属性.我也可以做到.

And then I can purportedly use this variable in my template for "num" to generate the desired "gcd" element along with attribute. That too I can do.

上面的循环应该是什么样的?如果不是一个for-each,假设我已经有两个元素的gcd模板,那么我将如何实际计算XML文档中所有"num"个元素的gcd?

What should the above loop be like? If not a for-each, how would I actually compute the gcd of all the "num" elements in my XML document assuming I already have a working gcd template for two elements?

我想如果我将所有"num"元素都作为兄弟姐妹或其他东西的话,这会容易得多,但不幸的是我没有.其中任何两个之间的树关系可能是任意的.我唯一知道的是,它们都具有相同的名称.

I suppose this would be a lot easier if I had all the "num" elements as siblings or something but unfortunately I don't. The tree relationship between any two of these could potentially be arbitrary. The only thing I know, is that they all have the same name.

推荐答案

XSLT是一种功能语言,因此没有可更新变量的概念-变量在创建时已设置其值,并且该值无法随后更改.如果必须在纯XSLT中执行此操作,则需要将问题重新构造为递归模板而不是循环

XSLT is a functional language, so there's no concept of updatable variables - a variable has its value set when it is created, and that value cannot be changed subsequently. If you must do this in pure XSLT you'll need to reformulate the problem as a recursive template instead of a loop

<xsl:template name="gcd">
  <xsl:param name="nodes" />
  <xsl:choose>
    <xsl:when test="count($nodes) = 0">0</xsl:when>
    <xsl:otherwise>
      <xsl:variable name="gcdOfRest">
        <xsl:call-template name="gcd">
          <xsl:with-param name="nodes" select="$nodes[position() > 1]" />
        </xsl:call-template>
      </xsl:variable>
      <!-- existing logic goes here, calculating GCD between number($nodes[1])
           and number($gcdOfRest) -->
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

并通过传递//ns0:num 作为 nodes 参数来调用此模板.

and call this template passing //ns0:num as the nodes param.

但是,如果您可以选择编写扩展功能并将其插入,则可能会更有效.

But if you have the option of writing an extension function and plugging it in, that may be more efficient.

这篇关于循环使用临时值在xsl中设置一组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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