Xslt - 以块的形式迭代节点 [英] Xslt - iterate nodes in chunks

查看:22
本文介绍了Xslt - 以块的形式迭代节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 xslt 脚本,它创建一个带有 FOP 的 PDF,问题是当节点数超过某个限制时,它在特定节点类型上执行应用模板时内存不足.为了解决内存问题,我需要将来自该模板调用的结果分解为多个 <fo:page-sequence>,但我不希望每个节点都有一个页面序列,更多就像每 100 个节点一样.

I have an existing xslt script which creates a PDF with FOP, problem is that its running out of memory when doing apply-template on a specific nodetype when the node count exceeds a certain limit. In order to fix the memory issue I need to break up the result derived from that template call into several <fo:page-sequence>, but I dont want a page-sequence for each node, more like for each 100 node.

我的第一个想法是使用 position() 并简单地每 100 次添加新的序列标签,但由于 xslt 需要格式良好,我不能有这样的开放标签.

My first thought was to use position() and simply add new sequence tags every 100th, but as xslt needs to be well formed I cant have open tags like that.

所以,我的问题是,以 100 个块为单位迭代特定类型的所有节点的最佳替代方法是什么?

So, my question is what would be the best alternative to iterate all nodes of a specific type in chunks of 100?

XML 源代码示例

 <var ID="V1"><subnodes/></var>  
 ..
 <var ID="V1000"><subnodes/></var>

目前正在处理

<fo:page-sequence>
  <xsl:apply-templates select="ns:var"/>
</fo:page-sequence>

想要的 FOP XML 结果将创建多个页面序列,每个页面序列中包含(例如)100 个已处理的 var 节点,而不是在一个页面序列中包含所有 1000 个的结果.

The wanted FOP XML result would be something which created several page-sequences with (for example) 100 of processed var nodes in each instead of having the result of all 1000 in óne page-sequence.

当前结果

<fo:page-sequence>
  <formatted V1>
  ..
  <formatted V1000>
</fo:page-sequence>

想要的结果

<fo:page-sequence>
   <formatted V1>
    ..
   <formatted V100>
</fo:page-sequence>

<fo:page-sequence>
  <formatted V101>
   ..
  <formatted V200>
 </fo:page-sequence>

..

<fo:page-sequence>
  <formatted V901>
   ..
  <formatted V1000>
</fo:page-sequence>   

推荐答案

编辑:抱歉输入错误.following-sibling

使用此输入:

<root>
    <var ID="V1">
        <subnodes/>
    </var>
    <var ID="V2">
        <subnodes/>
    </var>
    <var ID="V3">
        <subnodes/>
    </var>
    <var ID="V4">
        <subnodes/>
    </var>
    <var ID="V5">
        <subnodes/>
    </var>
    <var ID="V6">
        <subnodes/>
    </var>
    <var ID="V7">
        <subnodes/>
    </var>
    <var ID="V8">
        <subnodes/>
    </var>
</root>

这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match="root">
        <xsl:copy>
            <xsl:copy-of select="document('')/*/namespace::*[name()!='xsl']"/>
            <!-- Forget above. It's just to prettify namespace fixup -->
            <xsl:for-each select="var[position() mod 3 = 1]">
                <fo:page-sequence>
                    <xsl:apply-templates
             select=".|following-sibling::var[3 > position()]"/>
                </fo:page-sequence>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="var">
        <formatted id="{@ID}"/>
    </xsl:template>
</xsl:stylesheet>

输出:

<root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:page-sequence>
        <formatted id="V1" />
        <formatted id="V2" />
        <formatted id="V3" />
    </fo:page-sequence>
    <fo:page-sequence>
        <formatted id="V4" />
        <formatted id="V5" />
        <formatted id="V6" />
    </fo:page-sequence>
    <fo:page-sequence>
        <formatted id="V7" />
        <formatted id="V8" />
    </fo:page-sequence>
</root>

这篇关于Xslt - 以块的形式迭代节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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