[英]Sequentially group / process contents with XSLT 2.0 / XSL-FO
元
我必须做两件事:
1.)我想为某些在(x)之后不跟随的节点(p)应用模板。
因此,对于以下示例,唯一应处理的节点应该是第一个(p) 。
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.)我还想将所有(p)个节点的内容作为每个(x)之后的组进行处理。
因此,对于上面的示例,每个x之后的内容都应放入block元素中(下文)。
<xsl:template match="x">
<fo:block>
<!-- content from following (p) nodes until the next following (x) -->
</fo:block>
</xsl:template>
有没有简单的方法可以对群组或交叉点进行此操作?
您没有显示任何父元素,但为该父元素编写了一个模板:
<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>
如果您对所有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>
但是,如果您确实确实想对第一个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>
因为在xsl:for-each-group
的迭代中, current-group()
仍然可以在其他模板中使用。
多亏了您,我找到了可行的解决方案。
我为父容器元素(如下)创建了一个模板:
<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>
还有一个x元素的模板(如下):
<xsl:template match="x">
<fo:block>
<xsl:apply-templates select="current-group()[position() gt 1]"/>
</fo:block>
</xsl:template>
结果
如果前面的兄弟是x,则按组处理内容; 如果不是的话,也要定期(但不属于任何x组的一部分)。
这些事情很难描述-感谢您的耐心配合!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.