Xslt-迭代块中的节点 [英] Xslt - iterate nodes in chunks
问题描述
我有一个现有的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
轴后的未接节点测试
Edit: Sorry for typo. Miss node test after following-sibling
axis
使用此输入:
<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屋!