1

我正在为一些基本的 xslt 分组而苦苦挣扎,但不明白哪里出了问题。拍摄以下 xml 快照:

<ul>
<li>This is a text only node</li>
<li>This one contains some different ones, like an  <img src="#" alt="dummy" />image and a break.<br />Also a link <a href="#"> for testing purposes</a>.</li>
<li>This one contains some different ones, like an  <img src="#" alt="dummy" />image but no break. Also a link <a href="#"> for testing purposes</a>.</li>
</ul>

我想要实现的是,对于每个包含中断的 [li],在中断被包装之前的内容,如果它不包含中断,则整个内容都会被包装。所以想要的结果如下:

<ul>
<li>
    <string>This is a text only node</string>
</li>
<li>
    <string>This one contains some different ones, like an  <img src="#" alt="dummy" />image and a break.</string>
    <br/>
    <string>Also a link <a href="#"> for testing purposes</a>.
</li>
<li>
    <string>This one contains some different ones, like an  <img src="#" alt="dummy"/>image but no break. Also a link <a href="#"> for testing purposes</a>.</string>
</li>

这是我的 xslt

    <xsl:template match="li">
<xsl:copy>
    <xsl:choose>
        <xsl:when test="br">
        <!-- node contains a break so let's group -->
            <xsl:for-each-group select="*" group-ending-with="br">
                <!-- copy all but the break -->
                <string><xsl:copy-of select="current-group()[not(self::br)]"/></string>
                <!-- and place break unless it's the last element, then we don't need it... -->
                <xsl:if test="not(position() = last())"><br/></xsl:if>
            </xsl:for-each-group>
        </xsl:when>
        <xsl:otherwise>
            <string><xsl:apply-templates select="@* | node()"/></string>
        </xsl:otherwise>
    </xsl:choose>
    </xsl:copy>
</xsl:template>

但这似乎也剥夺了我的文字,这不是我想要的......我得到的输出如下:

<ul>
<li>
    <string>This is a text only node</string>
</li>
<li>
    <string>
        <img src="#" alt="dummy"/>
    </string>
    <br/>
    <string>
        <a href="#"> for testing purposes</a>
    </string>
</li>
<li>
    <string>This one contains some different ones, like an  <img src="#" alt="dummy"/>image but no break. Also a link <a href="#"> for testing purposes</a>.</string>
</li>

所以它会从 current-group() 中删除我的文本。我在这里俯瞰什么?

4

1 回答 1

2

好吧,您需要处理所有子节点,<xsl:for-each-group select="node()" group-ending-with="br">而不是像当前那样简单地处理元素子节点。

于 2013-04-29T11:01:00.387 回答