2016-06-11 2 views
0

XML (실제로는 XHTML) 문서를 섹션의 최상위 레벨 h1 태그로 분할하고 싶습니다. <h1>의 첫 번째 문자부터 시작하여 다음 문자까지의 문자는 모두 <section> 요소로 감싸 져야합니다. 나는이 소스 문서가있는 경우XSLT 1.0을 사용하여 두 태그 발생 사이의 모든 것을 마무리하십시오.

예를 들어, :

<article> 
    <h1>Heading 1</h1> 
    <p>Some text</p> 
    <p>Some more text</p> 

    <h1>Heading 2</h1> 
    <p>Some text</p> 
    <h2>Subheading</h2> 
    <p>Some text</p> 

    <h1 id="heading3">Heading 3</h1> 
    <p>Some text</p> 
</article> 

을 나는 결과가 정확히처럼되고 싶어 :

<article> 
    <section> 
     <h1>Heading 1</h1> 
     <p>Some text</p> 
     <p>Some more text</p> 
    </section> 
    <section> 
     <h1>Heading 2</h1> 
     <p>Some text</p> 
     <h2>Subheading</h2> 
     <p>Some text</p> 
    </section> 
    <section> 
     <h1 id="heading3">Heading 3</h1> 
     <p>Some text</p> 
    </section> 
</article> 

문제는 내가 가진 모든 것은 따라서 libxslt1.1 (이다 , XSLT 1.0 + EXSLT). XSLT 2.0을 사용하면 멋지게 보이는 <xsl:for-each-group select="*" group-starting-with="h1">으로 뭔가를 할 수 있었지만 슬프게도 나를 위해 실행 가능한 옵션이 아닙니다.

속성 값을 그룹화하고 싶지 않습니다 (의미있는 속성이 없음). 이해할 수있는대로, Muenchian 그룹화는 저에게 도움이되는 트릭이 아닙니다. 어쩌면 내가 틀 렸습니다. 몇 분 전에이 방법에 대해서 읽었을뿐입니다.

XSLT 1.0에서이를 수행 할 수있는 방법이 있습니까?

답변

2

내가 알고있는 것처럼, Muenchian 그룹화는 나를 위해 을 작동시키는 트릭이 아닙니다.

음, 않습니다 그것은 매우 가까운 무언가 :

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:key name="grpById" match="*[not(self::h1)]" use="generate-id(preceding-sibling::h1[1])" /> 

<xsl:template match="/article"> 
    <xsl:copy> 
     <xsl:for-each select="h1"> 
      <section> 
       <xsl:copy-of select=". | key('grpById', generate-id())"/> 
      </section> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

감사합니다. 완벽하게 작동합니다. 나는'generate-id (선행 형제 :: h1 [1])'에 대해 몰랐다. 항상 새로운 것을 배울 수 있습니다. – drdaeman

2

사용 핵심 name="group" match="article/*[not(self::h1)]" use="count(preceding-sibling::h1)", 다음 articleh1 자식 요소와의 템플릿을 적용 일치하는 템플릿 h1 템플릿을 만들고 섹션을 만들고 .|key('group', position())을 복사하십시오.

관련 문제