0

我在尝试翻译的 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 循环函数按预期工作,但第二个似乎没有。如果我在第二个 for 循环中使用<xsl:copy-of> 输出 > 中的第一个元素,它会显示> 元素,该元素甚至不应该在组中。<current-group<h2

如果有人能指出我哪里出错了,或者提供更好的解决方案,将不胜感激。

4

2 回答 2

0

我认为您已经简化了问题,并且这样做引入了一些红鲱鱼。

xsl:apply-templates select="h2"肯定什么都不做,因为在外部分组中选择的节点都没有 h2 子节点。

根据定义,在外部 for-each-group 选择的每个组中,除了第一个,组中的第一个节点将是一个 h2 元素。您的内部 for-each-group 将以 h2 开头的节点序列划分为:首先,以 h2 开头的组(因为每个节点都成为某个组的一部分),然后是一组序列,每个组都以一个h3。您需要拆分第一个(非 h3)组并区别对待,因为在这种情况下您不想生成container元素。因此,您需要在内部 for-each-group 中使用 xsl:choose,通常使用条件xsl:when test="self::h2"来检测您正在处理特殊的第一组。

说了这么多,我不明白为什么你没有container为每个 h3 元素获取一个元素。我认为这一定是由您没有向我们展示的某些东西引起的(可能是命名空间问题?)

于 2013-07-03T21:20:12.350 回答
0

我想你想改变

<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处理h3table元素,但不处理h2开始外部组的元素。

如果您需要更多帮助,请考虑发布带有命名空间的小而完整的示例,以便我们重现带有不想要的输出的问题。

于 2013-07-04T10:41:08.460 回答