2017-12-31 257 views
1

유사한 태그 항목에서 '장'또는 '그룹'다음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> 

내가으로이 XML 파일을 사전 처리하고 마지막으로 PDF로 출력하기 전에 XSL 3.0 을 사용하는 다른 형식의 XML 변환의 일환으로 front/group/@n 값을 반영하는 의 새 <chapter> 요소를 수집하고 '포장'합니다. 새로운 코퍼스는 group/@n 값이 새 chapter에 따라 그룹화하는 논리를 제공하는 다음과 같이 보일 것이다 :

파일이 이미 등 foo_group_A, foo_group_B을 사전 정렬
<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> 

, 그래서 별도의 정렬이 필요하지 않습니다. 관련 문서를 포함하려면 <chapter>이라는 새 요소를 만들어야합니다. 나는 이것을 xsl:for-each으로 시도했지만 반복 할 그룹의 '요약'또는 '모음'을 놓치고 있다고 생각합니다.

미리 감사드립니다.

답변

3

XSLT 3을 사용하고 항목을 그룹화하려면 물론 xsl:for-each 대신 xsl:for-each-group을 사용하면됩니다. document의 이미 그룹화 키 front/group/@n으로 분류하는 경우

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

는 대신 group-by 위의 xsl:for-each-group select="document" group-adjacent="front/group/@n"을 사용하는 것이 충분해야하고 그 다음 쉬울 것 그 방법에 의해 거대한 문서 스트리밍 사용 streamable="yes"xsl:mode 선언에 추가하고 그룹화에 xsl:for-each-group select="copy-of(document)" group-adjacent="front/group/@n"을 사용합니다.