XSLT 每第 n 个节点都有一个过滤器
XSLT every nth node with a filter
我正在使用 XSLT 进行某些输出格式设置,我希望在输出的每 N 个节点周围都有一个包装器元素。我读过 xslt - adding </tr><tr> every n node?,但我的问题是源节点必须来自查找:
<xsl:for-each select="key('items-by-product', $productid)">
而不仅仅是模板匹配。我发现的所有示例都假设您想要的节点都彼此相邻,并且它们只是在计算兄弟姐妹。
我有一个适合我的解决方案:
<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 < $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>
但是这是循环遍历每个节点的所有节点,这让我觉得效率很低。
是否有更好的方法可以更有效地产生相同的输出?以下兄弟技术可以与过滤器一起使用吗?
您可以从复制感兴趣的节点到一个变量开始;这将使他们——而且只有他们——成为手足。但是,在 XSLT 1.0 中,这样的变量将包含一个 result-tree-fragment,需要先将其转换为 node-set进一步处理:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="items-by-product" match="item" use="productid"/>
<xsl:variable name="groupSize" select="2" />
<xsl:variable name="productid" select="1" />
<xsl:variable name="my-items">
<xsl:copy-of select="key('items-by-product', $productid)"/>
</xsl:variable>
<xsl:template match="/">
<output>
<xsl:for-each select="exsl:node-set($my-items)/item[position() mod $groupSize = 1]">
<outer pos="{position()}">
<xsl:for-each select=". | following-sibling::item[position() < $groupSize]" >
<inner>
<xsl:value-of select="itemid"/>
</inner>
</xsl:for-each>
</outer>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
您可以使用带有 position() mod $num_per_div
的外部循环来为每个块获得一个 "iteration",然后在该 select 内从整个 key(...)
中取出该块的成员] 节点由他们的位置设置:
<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() >= $first and position() <= $last]">
<inner><xsl:value-of select="itemid"/></inner>
</xsl:for-each>
</outer>
</xsl:for-each>
这里的关键是要记住 position()
函数是上下文相关的,在不同的时间有不同的含义。在$iter
变量的定义中,当前节点列表是外层for-each的节点select,即第一个,第三个的列表, fifth, 等等 键返回的项目(所以 position()
表示 chunk 号)。但是在 inner 的 select
的谓词中,for-each 当前节点列表是 all 从 key
函数调用(因此 position()
是被测节点在具有给定 productid
的所有节点列表中的位置)。
我正在使用 XSLT 进行某些输出格式设置,我希望在输出的每 N 个节点周围都有一个包装器元素。我读过 xslt - adding </tr><tr> every n node?,但我的问题是源节点必须来自查找:
<xsl:for-each select="key('items-by-product', $productid)">
而不仅仅是模板匹配。我发现的所有示例都假设您想要的节点都彼此相邻,并且它们只是在计算兄弟姐妹。
我有一个适合我的解决方案:
<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 < $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>
但是这是循环遍历每个节点的所有节点,这让我觉得效率很低。
是否有更好的方法可以更有效地产生相同的输出?以下兄弟技术可以与过滤器一起使用吗?
您可以从复制感兴趣的节点到一个变量开始;这将使他们——而且只有他们——成为手足。但是,在 XSLT 1.0 中,这样的变量将包含一个 result-tree-fragment,需要先将其转换为 node-set进一步处理:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="items-by-product" match="item" use="productid"/>
<xsl:variable name="groupSize" select="2" />
<xsl:variable name="productid" select="1" />
<xsl:variable name="my-items">
<xsl:copy-of select="key('items-by-product', $productid)"/>
</xsl:variable>
<xsl:template match="/">
<output>
<xsl:for-each select="exsl:node-set($my-items)/item[position() mod $groupSize = 1]">
<outer pos="{position()}">
<xsl:for-each select=". | following-sibling::item[position() < $groupSize]" >
<inner>
<xsl:value-of select="itemid"/>
</inner>
</xsl:for-each>
</outer>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
您可以使用带有 position() mod $num_per_div
的外部循环来为每个块获得一个 "iteration",然后在该 select 内从整个 key(...)
中取出该块的成员] 节点由他们的位置设置:
<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() >= $first and position() <= $last]">
<inner><xsl:value-of select="itemid"/></inner>
</xsl:for-each>
</outer>
</xsl:for-each>
这里的关键是要记住 position()
函数是上下文相关的,在不同的时间有不同的含义。在$iter
变量的定义中,当前节点列表是外层for-each的节点select,即第一个,第三个的列表, fifth, 等等 键返回的项目(所以 position()
表示 chunk 号)。但是在 inner 的 select
的谓词中,for-each 当前节点列表是 all 从 key
函数调用(因此 position()
是被测节点在具有给定 productid
的所有节点列表中的位置)。