2017-12-31 245 views
1

から「章」または「グループ」を作成:私はには、このXMLファイルを前処理していますXSLは、私は、一般的に次のような構造を持つ大規模なXMLコーパスの文書持つ類似したタグ付きエントリ

<corpus> 
    <document n="001"> 
     <front> 
      <title>foo title</title> 
      <group n="foo_group_A"/> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
      <seg n="3">some text with markups</seg> 
     </body> 
    </document> 
    <document n=002"> 
     <front> 
      <title>foo title</title> 
      <group n="foo_group_A"/> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
     </body> 
    </document> 
    <document n="003"> 
     <front> 
      <title>foo title</title> 
      <group n="foo_group_A"/> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
      <seg n="3">some text with markups</seg> 
     </body> 
    </document> 
    <document n="004"> 
     <front> 
      <title>foo title</title> 
      <group n="foo_group_B"/> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
     </body> 
    </document> 
    <document n="005"> 
     <front> 
      <title>foo title</title> 
      <group n="foo_group_B"/> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
     </body> 
    </document> 
    [...] 
</corpus> 

を最終的にPDFに出力する前に、XSL 3.0 を使用した別の形式のXML変換の一環として、<document><chapter>要素に、front/group/@nの値を反映させて「ラップ」したいと思います。新しいコーパスはgroup/@n値が新しいchapterの下にグループ化するためのロジックを提供する場合、次のようになります。

<corpus> 
    <chapter n="foo_group_A"> 
    <document n="001"> 
     <front> 
      <title>foo title</title> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
      <seg n="3">some text with markups</seg> 
     </body> 
    </document> 
    <document n=002"> 
     <front> 
      <title>foo title</title> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
     </body> 
    </document> 
    <document n="003"> 
     <front> 
      <title>foo title</title> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
      <seg n="3">some text with markups</seg> 
     </body> 
    </document> 
    </chapter> 
    <chapter n="foo_group_B"> 
    <document n="004"> 
     <front> 
      <title>foo title</title> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
     </body> 
    </document> 
    <document n="005"> 
     <front> 
      <title>foo title</title> 
     <front> 
     <body> 
      <seg n="1">some text with markups</seg> 
      <seg n="2">some text with markups</seg> 
     </body> 
    </document> 
    </chapter> 
    [...] 
</corpus> 

ファイルがすでになど事前ソートfoo_group_A、foo_group_B、ですので、余分なソートは必要ありません。関連する文書を含めるには、新しい要素<chapter>を作成するだけです。私はxsl:for-eachでこれを試しましたが、私は、いくつかの種類の 'サマリー'または反復するグループの 'コレクション'がないと思います。

事前に感謝します。

答えて

3

XSLT 3を使用していてアイテムをグループ化する場合は、もちろんxsl:for-eachではなくxsl:for-each-groupを使用します。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="3.0"> 

    <xsl:mode on-no-match="shallow-copy"/> 

    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="corpus"> 
     <xsl:copy> 
      <xsl:for-each-group select="document" group-by="front/group/@n"> 
       <chapter n="{current-grouping-key()}"> 
        <xsl:apply-templates select="current-group()"/> 
       </chapter> 
      </xsl:for-each-group> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="front/group"/> 

</xsl:stylesheet> 

http://xsltfiddle.liberty-development.net/nbUY4ki

document Sは、すでにそれはまたgroup-byxsl:for-each-group select="document" group-adjacent="front/group/@n"の代わりに使用することが十分であるグループ化キーfront/group/@nによってソートし、それまでに膨大な文書のためのストリーミングを使用することが容易になるだろうそのようにしている場合streamable="yes"xsl:mode宣言に追加し、グループ化にxsl:for-each-group select="copy-of(document)" group-adjacent="front/group/@n"を使用します。

関連する問題