2

'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>

私を正しい方向に向けることができるヒントを事前に感謝します!

4

2 に答える 2

1

これは単純化された例であるため、私の解決策はあなたが望むものではないかもしれないとあなたは言います。これにグループ化を使用しないようにしたことがありますか?次のXSLは正しい結果をもたらすようです。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output indent="yes" />

   <xsl:template match="/ | * | text() | comment() ">
      <xsl:copy>
         <xsl:apply-templates select="* | text() | comment() " />
      </xsl:copy>
   </xsl:template>

   <xsl:template match="p">
      <xsl:copy-of select="." />
   </xsl:template>

   <xsl:template match="Content/text()">
      <xsl:choose>
      <xsl:when test="preceding-sibling::Underline"></xsl:when>
      <xsl:when test="following-sibling::EndUnderline"></xsl:when>
      <xsl:otherwise>
      <xsl:copy-of select="." />
      </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <xsl:template match="Content/Underline" />

   <xsl:template match="Content/EndUnderline">
      <xsl:choose>
         <xsl:when test="preceding-sibling::Underline">
            <u><xsl:value-of select="preceding-sibling::text()[1]" /></u>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="preceding-sibling::text()[1]" />
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

</xsl:stylesheet>
于 2011-06-24T15:13:00.510 に答える
0

ありがとうございますが、これは実際には、開始タグと終了タグの間に単一の要素がある場合にのみ機能します。

とにかく、私は他の役立つインターネットの人々のおかげでその間に答えを見つけたので、私たちが最後に思いついたものを共有させてください:

        <xsl:template match="Content">
    <xsl:copy>
        <xsl:for-each-group select="node()" group-ending-with="EndUnderline">
            <xsl:variable name="start" select="current-group()[self::Underline][1]"/>
            <xsl:choose>
                <xsl:when test="$start">
                    <!-- Content element contains at least one <Underline/> marker element, so we group all between the first <Underline/> tag until the first <EndUnderline/> tag -->
                    <xsl:apply-templates select="current-group()[$start >> .]"/>
                    <!-- Every tag before the first <Underline/> marker gets transformed as standard, all tags between the markers gets encapsulated in a <u> tag -->
                    <u>
                        <xsl:apply-templates select="current-group()[. >> $start][not(self::Underline)][not(self::EndUnderline)]"/>
                    </u>
                </xsl:when>
                <xsl:otherwise>
                    <!-- Apply standard transformation on current group (not containing underline tags...) -->
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>
<!-- Get rif of standalone end tags... -->
<xsl:template match="EndUnderline"/>
于 2011-06-26T20:43:10.100 に答える