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>

そのため、現在のグループ()からテキストが削除されます。私はここで何を見落としていますか?

4

1 に答える 1

2

<xsl:for-each-group select="node()" group-ending-with="br">現在行っているように単に要素の子ノードを処理するのではなく、すべての子ノードを処理する必要があります。

于 2013-04-29T11:01:00.387 に答える