'for-each-group'を機能させるのに苦労しています。最近、xslt 2に切り替えましたが、すべてを理解するためにやるべきことがまだあります。Framemaker MIF(flat xml)から受け取ったいくつかのファイルをクリーンアップしようとしていますが、ほとんどの場合、データはかなりクリーンですが、例外が原因です。以下のxmlでいくつかの典型的な例を組み合わせました。私が使用する例は、下線タグに関連しています。原則として、ファイルは次のようにビルドされます。[Underline /]タグが表示された場合、[EndUnderline /]タグに到達するまで、後続のすべての兄弟に下線を付ける必要があるため、私の目的は次のとおりです。これらの両方のタグを取り除き、その間のすべての兄弟を1つの[u]タグにカプセル化します。ただし、問題は、実際の[EndUnderline/]タグに到達するまで無視する必要がある後続の[Underline/]タグが存在する可能性があることです。
上記をより見やすくしてみましょう。これは単純化されたXMLファイルです。
<TestFile>
<!-- Para tag containing no underline tags -->
<Para>
<Content>[text_not_underlined]</Content>
</Para>
<!-- correct encapsulation from source -->
<Para>
<Content>
<Underline/>[text_to_be_underlined]<EndUnderline/>
<p>Some test data</p>
</Content>
</Para>
<!-- extra underline tag that should be ignored -->
<Para>
<Content>
<Underline/>[text_to_be_underlined]
<Underline/>
<EndUnderline/>
<p>Some other test data</p>
</Content>
</Para>
<!-- some extra end underline tags that should be ignored -->
<Para>
<Content>
<EndUnderline/>[no_longer_underline]<EndUnderline/>
<p>: More data</p>
</Content>
</Para>
</TestFile>
これは私が今までxsltで得た場所です:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Content">
<xsl:copy>
<xsl:for-each-group select="node()" group-ending-with="EndUnderline">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:variable name="start" select="current-group()[self::Underline][1]"/>
<xsl:copy-of select="current-group()[$start >> .]"/>
<u>
<xsl:copy-of select="current-group()[. >> $start][not(self::Underline)][not(self::EndUnderline)]"/>
</u>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
そしてこれが結果です:
<TestFile>
<!-- Para tag containing no underline tags -->
<Para>
<Content>
<u/>
</Content>
</Para>
<!-- correct encapsulation from source -->
<Para>
<Content>
<u>[text_to_be_underlined]</u>
<u/>
</Content>
</Para>
<!-- extra underline tag that should be ignored -->
<Para>
<Content>
<u>[text_to_be_underlined]</u>
<u/>
</Content>
</Para>
<!-- some extra end underline tags that should be ignored -->
<Para>
<Content>
<u/>
<u/>
</Content>
</Para>
</TestFile>
これが私が目指しているものですが:
<TestFile>
<!-- Para tag containing no underline tags -->
<Para>
<Content>[text_not_underlined]</Content>
</Para>
<!-- correct encapsulation from source -->
<Para>
<Content>
<u>[text_to_be_underlined]</u>
<p>Some test data</p>
</Content>
</Para>
<!-- extra underline tag that should be ignored -->
<Para>
<Content>
<u>[text_to_be_underlined]</u>
<p>Some other test data</p>
</Content>
</Para>
<!-- some extra end underline tags that should be ignored -->
<Para>
<Content>
[no_longer_underline]
<p>: More data</p>
</Content>
</Para>
</TestFile>
私を正しい方向に向けることができるヒントを事前に感謝します!