我在翻译一个HTML文件时遇到了一些麻烦。基本上,当前源代码结构的相关部分是这样的:
<h2 />
<h3 />
<table />
<table />
<h3 />
<table />
<table />
<h3 />
<table />
<h3 />
<h3 />
<table />
<table />
<h2 />
<h3 />
...诸若此类。其中的每一个内容都以不同的方式被翻译,但我目前遇到的问题是如何正确地对它们进行分组。本质上,我希望它像下面这样结束:
<category>
<h2 />
<container>
<h3 />
<table />
<table />
</container>
<container>
<h3 />
<table />
<table />
</container>
<container>
<h3 />
<table />
</container>
<container>
<h3 />
</container>
<container>
<h3 />
<table />
<table />
</container>
</category>
<category>
<h2 />
<container>
<h3 />
...为了实现这一点,我使用了以下代码:
<xsl:for-each-group select="node()"group-starting-with="xh:h2">
<category>
<xsl:apply-templates select="xh:h2"/>
<xsl:for-each-group select="current-group()"
group-starting-with="xh:h3">
<container>
<xsl:apply-templates select="current-group()[node()]"/>
</container>
</xsl:for-each-group>
</category>
</xsl:for-each-group>然而,我从这里得到的输出如下:
<category>
<h2 />
<container>
<h3 />
<table />
<table />
<h3 />
<table />
<table />
<h3 />
<table />
<h3 />
<h3 />
<table />
<table />
</container>
</category>
<category>
<h2 />
<container>
<h3 />
...第一个for-loop函数按预期工作,但第二个函数似乎不是这样。如果我使用<xsl:copy-of>在第二个for循环中输出<current-group>中的第一个元素,它将显示<h2>元素,其中该元素甚至不应该在组中。
如果有人能指出我的错误所在,或者提供更好的解决方案,我将不胜感激。
发布于 2013-07-04 18:41:08
我觉得你想改变
<xsl:for-each-group select="node()" group-starting-with="xh:h2">
<category>
<xsl:apply-templates select="xh:h2"/>
<xsl:for-each-group select="current-group()"
group-starting-with="xh:h3">至
<xsl:for-each-group select="*" group-starting-with="xh:h2">
<category>
<xsl:apply-templates select="."/>
<xsl:for-each-group select="current-group() except ."
group-starting-with="xh:h3">这样,内部for-each-group将处理h3和table元素,但不会处理启动外部组的h2元素。
如果您需要更多帮助,请考虑发布带有名称空间的小但完整的示例,允许我们使用不需要的输出重现问题。
发布于 2013-07-04 05:20:12
我认为您已经简化了问题,并且在这样做的过程中引入了一些转移注意力的问题。
xsl:apply-templates select="h2"肯定什么也不做,因为在外部分组中选择的节点都没有h2子节点。
在由外部for-each-group选择的每个组中,根据定义,该组中的第一个节点将是一个h2元素。内部的for- each组将把以h2开头的节点序列划分为:首先,以h2开头的组(因为每个节点都成为某个组的一部分),然后是组序列,每个组都以h3开头。您需要拆分第一个(非h3)组,并以不同的方式对待它,因为在本例中您不希望生成container元素。因此,您需要在内部for-each-group中使用xsl:choose,通常使用条件xsl:when test="self::h2"来检测您正在处理的是特殊的第一个组。
说了这么多,我不明白为什么不为每个h3元素获取一个container元素。我认为这一定是由您没有向我们展示的一些东西引起的(可能是名称空间问题?)
https://stackoverflow.com/questions/17452590
复制相似问题