xsl索引的递归循环节点 [英] xsl recursive loop node by index

查看:219
本文介绍了xsl索引的递归循环节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个递归模板,用于从我的XML中获取前n个项目。

I have created a recursive template for getting the first n number of items from my XML.

它使用索引(计数器)就像我在for循环。现在我如何使用索引从我的XML中获取节点?

It uses an index(counter) just like how I would in a for loop. Now how can I get a node from my XML using the index?

我尝试过[position()= $ index]但是在尝试深入时它有奇怪的行为XML层次结构中的节点。

I have tried [position()=$index] but it had weird behaviour when trying to get deeper nodes in the XML hierarchy.

如果我有XML,例如:

If I have XML such as:

<0>
 <1>
  <2>item</2>
  <2>item</2>
  <2>item</2>
  <2>item</2>
  <2>item</2>
  <2>item</2>
 </1>
</0>

我希望能够计算并复制2,直到我有我想要的数量。

I want to be able to count through and copy the 2's until I have as many as I want.

推荐答案

您说要以n组为单位处理元素。以下XSLT 1.0解决方案:

You say you want to process your elements in groups of n. The following XSLT 1.0 solution does it:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:param name="pGroupCount" select="3" />

  <xsl:template match="/lvl-0">
    <xsl:copy>
      <!-- select the nodes that start a group -->
      <xsl:apply-templates mode="group" select="
        lvl-1/lvl-2[position() mod $pGroupCount = 1]
      " />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="lvl-2" mode="group">
    <!-- select the nodes belong to the current group -->
    <xsl:variable name="current-group" select="
      . | following-sibling::lvl-2[position() &lt; $pGroupCount]
    " />
    <!-- output the current group, you can also do calculations with it -->
    <group id="{position()}">
      <xsl:copy-of select="$current-group" />
    </group>
  </xsl:template>
</xsl:stylesheet>

当应用于此输入文档时:

When applied to this input document:

<lvl-0>
 <lvl-1>
  <lvl-2>item.0</lvl-2>
  <lvl-2>item.1</lvl-2>
  <lvl-2>item.2</lvl-2>
  <lvl-2>item.3</lvl-2>
  <lvl-2>item.4</lvl-2>
  <lvl-2>item.5</lvl-2>
  <a>foo</a><!-- to prove that position() calculations still work -->
  <lvl-2>item.6</lvl-2>
  <lvl-2>item.7</lvl-2>
  <lvl-2>item.8</lvl-2>
  <lvl-2>item.9</lvl-2>
 </lvl-1>
</lvl-0>

生成以下输出:

<lvl-0>
  <group id="1">
    <lvl-2>item.0</lvl-2>
    <lvl-2>item.1</lvl-2>
    <lvl-2>item.2</lvl-2>
  </group>
  <group id="2">
    <lvl-2>item.3</lvl-2>
    <lvl-2>item.4</lvl-2>
    <lvl-2>item.5</lvl-2>
  </group>
  <group id="3">
    <lvl-2>item.6</lvl-2>
    <lvl-2>item.7</lvl-2>
    <lvl-2>item.8</lvl-2>
  </group>
  <group id="4">
    <lvl-2>item.9</lvl-2>
  </group>
</lvl-0>

要明白这一点,你必须知道如何 position()有效。当像这样使用时:

To understand this you must know how position() works. When used like this:

lvl-1/lvl-2[position() mod $pGroupCount = 1]

它指的是 lvl-2 中的节点各自的(!)父母。在这种情况下,只有一个父级,因此 item.0 的位置为1, item.9 的位置为10 。

it refers to the position of lvl-2 nodes within their respective (!) parent. In this case, there is only one parent, so item.0 has position 1 and item.9 has position 10.

当像这样使用时:

following-sibling::lvl-2[position() &lt; $pGroupCount]

它指的是跟随兄弟的相对位置: :轴。在这种情况下, item.1 item.0 方面的相对位置为1。 (基本上,这与上面的相同,只是沿着(隐含的) child :: 轴计算。)

it refers to the relative position along the following-sibling:: axis. In this context, item.1 would have a relative position of 1 in regard to item.0. (Basically, this is the same as the above, which just counts along the (implicit) child:: axis.)

单独使用时,如下所示:

When used on its own, like here:

<group id="{position()}">

它指的是当前正在处理的批次中当前节点的位置。在我们的例子中,批处理由启动组的节点组成( item.0 item.3 item.6 item.9 ),因此从1到4。

it refers to the position of the current node in the batch that is being processed at the moment. In our case the "batch" consists of nodes that start a group (item.0, item.3, item.6, item.9), so it goes from 1 to 4.

这篇关于xsl索引的递归循环节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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