3

XSLT 2.0 で xsl:for-each を使用して要素をグループ化しようとしています。

これが私の入力の実例です:

<root>
     <chapter>
           <heading> heading text </heading>
           <title> title </title>
           <p> 1111 </p>
           <p> 2222 </p>
           <center> text </center>
           <p> 3333 </p>
           <title>  another title </title>
           <p> 4444 </p>
           <center> text </center>
           <p> 5555 </p>
     </chapter>
     <chapter>
          <heading> heading text </heading>
          <title>  another title </title>
          <p> 6666 </p>
          <p> 7777 </p>
          <title>  another title </title>
          <p> 8888 </p>
          <p> 9999 </p>
     </chapter>
<root>

<title>各要素を照合し、次の要素まで次のすべての要素を要素にグループ化することにより、このドキュメントをグループ化しようとして<title>います<section>。出力を次のように表示します。

<root>
     <chapter>
          <heading> Heading text </heading>
          <section>
               <title>  title </title>
               <p> 1111 </p>
               <p> 2222 </p>
               <center> text </center>
               <p> 3333 </p>
          </section>
          <section>
               <title>  title </title>
               <p> 4444 </p>
               <center> text </center>
               <p> 5555 </p>
          </section>
          <section>
               <title>  title </title>
               <p> 6666 </p>
               <p> 7777 </p>
               <center> text </center>
               <p> 8888 </p>
               <p> 9999 </p>
          </section>
     <chapter>
<root>

機能していない現在のテンプレート:

<xsl:template match="chapter">
    <chapter>
      <xsl:for-each-group select="*" group-starting-with="title">
             <section>
                <xsl:copy-of select="current-group()" />
             </section>
      </xsl:for-each-group>
    </chapter>             
</xsl:template>

上記のスタイルシートは、必要なセクションをグループ化しますが、何らかの理由で各<heading>要素を独自にグループ化します。<section>助言がありますか?

前もって感謝します。

4

3 に答える 3

4

私は使うだろう...

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />  

<xsl:template match="/*">
  <root>
    <xsl:for-each-group select="chapter" group-by="heading">
      <chapter>
        <xsl:copy-of select="current-group()[1]/heading" />
        <xsl:for-each-group select="current-group()/(* except heading)"
                            group-starting-with="title">
          <section>
            <xsl:copy-of select="current-group()" />
          </section>
        </xsl:for-each-group>
      </chapter>
    </xsl:for-each-group>
  </root>
</xsl:template>

</xsl:stylesheet>
于 2012-10-01T15:48:48.020 に答える
4

私は通常、for-each-group の本体で xsl:choose を使用して、実際に group-starting-with 要素で始まるグループと、最初の「実際のグループ」の前の「先頭のランプ」を区別します。「先頭のランプ」に見出し要素のみが含まれることがわかっている場合、うまく機能する他の多くのソリューションが提供されています。別のより一般的な解決策 (ランプにあるものについての仮定がないため) ですが、同じ方向に沿ったものは次のとおりです。

<xsl:copy-of select="title[1]/preceding-sibling::*">
<xsl:for-each-group select="title[1]/(., following-sibling::*)">
  <section>
     <xsl:copy-of select="current-group()"/>
  </section>
</xsl:for-each-group>
于 2012-10-01T19:32:49.110 に答える
3

あなたはおそらくしたいです

<xsl:copy-of select="current-group()" />

ではなくvalue-ofheading見出しについては、各章に 1 つの要素しかないことがわかっている場合は、次のように言えます。

<xsl:template match="chapter">
      <xsl:copy-of select="heading" />
      <xsl:for-each-group select="*[not(self::heading)]" group-starting-with="title">
             <section>
                <xsl:copy-of select="current-group()" />
             </section>
      </xsl:for-each-group>             
</xsl:template>

つまり、見出しを個別に取り出して、グループ化する要素のセットから除外します。

于 2012-10-01T15:26:30.840 に答える