XSLT 每第 n 个节点带有一个过滤器 [英] XSLT every nth node with a filter

查看:25
本文介绍了XSLT 每第 n 个节点带有一个过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 XSLT 用于某些输出格式,并且我想要一个围绕输出的每 N 个节点的包装元素.我已经阅读了 xslt - 添加 </tr><tr>每个 n 节点?,但我的问题是源节点必须来自查找:

I'm using XSLT for some output formatting, and I want a wrapper element around every N nodes of the output. I've read xslt - adding </tr><tr> every n node?, but my problem is that the source nodes have to come from a lookup:

<xsl:for-each select="key('items-by-product', $productid)">

而不仅仅是模板匹配.我发现的所有示例都假设您想要的节点都彼此相邻,并且它们只是计算兄弟节点.

rather than just a template match. All the examples I've found assume that the nodes you want are all next to each other, and they're just counting siblings.

我有一个适合我的解决方案:

I have a solution that works for me:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:variable name='num_per_div' select='2' />
  <xsl:variable name='productid' select='1' />
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="items-by-product" match="item" use="productid"/>
  <xsl:template match="data">
    <output>
      <xsl:for-each select="key('items-by-product', $productid)">
        <xsl:variable name='pos' select='position()' />
        <xsl:if test="position() = 1 or not((position()-1) mod $num_per_div)">
          <outer pos="{$pos}">
            <xsl:for-each select="key('items-by-product', $productid)">
              <xsl:variable name='ipos' select='position()' />
              <xsl:if test="$ipos >= $pos and $ipos &lt; $pos + $num_per_div">
                <inner>
                  <xsl:value-of select="itemid"/>
                </inner>
              </xsl:if>
            </xsl:for-each>
          </outer>
        </xsl:if>
      </xsl:for-each>      
    </output>
  </xsl:template>
</xsl:stylesheet>

有数据

<data>
  <item>
    <productid>1</productid>
    <itemid>1</itemid>
  </item>
  <item>
    <productid>1</productid>
    <itemid>2</itemid>
  </item>
  <item>
    <productid>2</productid>
    <itemid>A</itemid>
  </item>
  <item>
    <productid>1</productid>
    <itemid>3</itemid>
  </item>
  <item>
    <productid>2</productid>
    <itemid>B</itemid>
  </item>
  <item>
    <productid>1</productid>
    <itemid>4</itemid>
  </item>
</data>

产生

<?xml version="1.0" encoding="utf-8"?>
<output>
  <outer pos="1">
    <inner>1</inner>
    <inner>2</inner>
  </outer>
  <outer pos="3">
    <inner>3</inner>
    <inner>4</inner>
  </outer>
</output>

但是这是遍历每个节点的所有节点,这让我觉得效率低下.

But this is looping through all the nodes for each node, which strikes me as inefficient.

有没有更好的方法可以更有效地产生相同的输出?以下同级技术可以与过滤器一起使用吗?

Is there a better approach that will produce the same output more efficiently? Can the following-sibling techniques work with a filter?

推荐答案

您可以使用带有 position() mod $num_per_div 的外循环来为每个块获得一个迭代",然后在该选择中从由它们的位置设置的整个 key(...) 节点中取出该块的成员:

You can use an outer loop with position() mod $num_per_div to get one "iteration" per chunk, then within that select out the members of that chunk out of the whole key(...) node set by their position:

<xsl:for-each select="key('items-by-product', $productid)
                       [position() mod $num_per_div = 1]">
  <xsl:variable name="iter" select="position()" />
  <xsl:variable name="first" select="($iter - 1) * $num_per_div + 1" />
  <xsl:variable name="last" select="$iter * $num_per_div" />
  <outer pos="{$first}">
    <xsl:for-each select="key('items-by-product', $productid)
                           [position() &gt;= $first and position() &lt;= $last]">
      <inner><xsl:value-of select="itemid"/></inner>
    </xsl:for-each>
  </outer>
</xsl:for-each>

这里的关键是要记住 position() 函数是上下文敏感的,在不同的时间意味着不同的东西.在$iter变量的定义中,当前节点列表是外部for-each选择的节点,即有第一个、第三个、第五个等的列表. 键返回的项目(所以 position() 表示 chunk 编号).但是在 inner for-each 的 select 的谓词中,当前节点列表是 allkey<返回的节点/code> 函数调用(所以 position() 是被测节点在具有给定 productid 的所有节点列表中的位置).

The key thing here is to remember that the position() function is context-sensitive and means different things at different times. In the definition of the $iter variable, the current node list is the nodes selected by the outer for-each, i.e. the list with the first, third, fifth, etc. items returned by the key (so position() means the chunk number). But in the predicate on the select of the inner for-each the current node list is all the nodes returned from the key function call (so position() is the position of the node-under-test within the list of all nodes with the given productid).

这篇关于XSLT 每第 n 个节点带有一个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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