[英]Sequentially group / process contents with XSLT 2.0 / XSL-FO
Meta 元
I have to do two things: 我必须做两件事:
1.) I want to apply templates for certain nodes (p) that do not follow after a (x). 1.)我想为某些在(x)之后不跟随的节点(p)应用模板。
So for the following example the only node that should be processed should be the first (p) . 因此,对于以下示例,唯一应处理的节点应该是第一个(p) 。
XML XML格式
<p>example...</p>
<x>example...</x>
<p>example...</p>
<p>example...</p>
<p>example...</p>
<x>example...</x>
<p>example...</p>
<p>example...</p>
2.) I also want to process the contents for all (p) nodes as groups that follow after each (x). 2.)我还想将所有(p)个节点的内容作为每个(x)之后的组进行处理。
So for the example above the contents that follow after each x should be put into the block element (s. below). 因此,对于上面的示例,每个x之后的内容都应放入block元素中(下文)。
<xsl:template match="x">
<fo:block>
<!-- content from following (p) nodes until the next following (x) -->
</fo:block>
</xsl:template>
Is there a simple way to do this with groups or intersections? 有没有简单的方法可以对群组或交叉点进行此操作?
You have not shown any parent element but write a template for that parent: 您没有显示任何父元素,但为该父元素编写了一个模板:
<xsl:template match="div[x]">
<fo:block>
<xsl:for-each-group select="*" group-starting-with="x">
<xsl:choose>
<xsl:when test="self::x">
<fo:block>
<xsl:copy-of select="current-group()[position() gt 1]"/>
</fo:block>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</fo:block>
</xsl:template>
Sample is at http://xsltransform.net/gWvjQeW . 样本位于http://xsltransform.net/gWvjQeW 。
If you're doing the same thing for all groups of p
and you really don't care about the x
elements, you could do: 如果您对所有p
组都执行相同的操作,而您实际上并不关心x
元素,则可以执行以下操作:
<xsl:template match="foo">
<xsl:for-each-group select="p" group-adjacent="self::p">
<fo:block>
<xsl:apply-templates select="current-group()"/>
</fo:block>
</xsl:for-each-group>
</xsl:template>
but if you really did want to do something different for the first p
(or multiple first p
): 但是,如果您确实确实想对第一个p
(或多个第一个p
)执行不同的操作:
<xsl:template match="foo">
<xsl:for-each-group select="p" group-adjacent="self::p">
<xsl:apply-templates select="."/>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p[empty(preceding-sibling::x)]">
<fo:block font-family="serif">
<xsl:value-of select="current-group()"/>
</fo:block>
</xsl:template>
<xsl:template match="p">
<fo:block>
<xsl:value-of select="current-group()"/>
</fo:block>
</xsl:template>
since current-group()
still works in other templates while you're within an iteration of the xsl:for-each-group
. 因为在xsl:for-each-group
的迭代中, current-group()
仍然可以在其他模板中使用。
Thanks to you I found a working solution. 多亏了您,我找到了可行的解决方案。
I created a template for the parent container element (s. below): 我为父容器元素(如下)创建了一个模板:
<xsl:template match="container">
<xsl:for-each group select="*" group-starting-with="x">
<xsl:choose>
<xsl:when test="self::x">
<xsl:apply-templates select="current-group()[position() = 1]"/>
</xsl:when>
<xsl:otherwise
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
And additionally a template for the x element (s.below): 还有一个x元素的模板(如下):
<xsl:template match="x">
<fo:block>
<xsl:apply-templates select="current-group()[position() gt 1]"/>
</fo:block>
</xsl:template>
Result 结果
Contents are processed in groups if preceding sibling is x; 如果前面的兄弟是x,则按组处理内容; and also regulary if not but not as part of any x group. 如果不是的话,也要定期(但不属于任何x组的一部分)。
These things are quiet hard to describe - Thank you for your patience! 这些事情很难描述-感谢您的耐心配合!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.