2014-12-10 4 views
0

다음 프로세스의 HTML이 있습니다.XSL에서 제목 수준을 그룹화하는 방법

<p class="Ahead">Heading 1</p> 
<p class="txt">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> 
<p class="Bhead">Heading 1.1</p> 
<p class="txt">Nibh euismod tincidunt ut laoreet dolore magna aliquam</p> 
<p class="list">Investigationes demonstraverunt lectores legere</p> 
<p class="Chead">Heading 1.1.1</p> 
<p class="txt">Typi non habent claritatem insitam</p> 
<p class="Bhead">Heading 1.2</p> 
<p class="txt">Lorem ipsum dolor sit amet</p> 

제목은 최대 5 단계로 진행됩니다. 서로 다른 속성이 제목 사이에 있습니다.

출력은 다음과 같습니다.

<section> 
    <section-title>Heading 1</section-title> 
    <p class="txt">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> 
    <section> 
    <section-title>Heading 1.1</section-title> 
    <p class="txt">Nibh euismod tincidunt ut laoreet dolore magna aliquam</p> 
    <p class="list">Investigationes demonstraverunt lectores legere</P> 
    <section> 
     <section-title>Heading 1.1.1</section-title> 
     <p class="txt">Typi non habent claritatem insitam</p> 
    </section> 
    </section> 
    <section> 
    <section-title>Heading 1.2</section-title> 
    <p class="txt">Lorem ipsum dolor sit amet</p> 
    </section> 
</section> 

미리 감사드립니다.

+0

당신은에 정교한 아래의 스타일 시트가 당신의 입력 XML에 적용되는

(나는 당신의 예를 입력 XML에 root라는 루트 노드를 추가 한) 너는 무엇과 붙어 있니? – Saurav

+0

정확하게 레벨을 결정하는 것은 무엇입니까? –

+0

섹션 태그를 사용하여 제목 수준을 그룹화하고 섹션 태그 계층 구조를 따르는 방법. – Shanmugalakshmi

답변

2

이 스타일 시트는 XSLT 2.0을 사용하고 마이클 케이의 XSLT 2.0 및 XPath는 2.0 프로그래머 Reference.pdf, 페이지 341-342에서 적응했다.

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* , node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/root"> 
     <xsl:for-each-group select="*" group-starting-with="p[@class='Ahead']"> 
      <xsl:choose> 
       <xsl:when test="self::p[@class='Ahead']"> 
        <xsl:apply-templates select="." mode="group"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:apply-templates select="current-group()"/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each-group> 
    </xsl:template> 

    <xsl:template match="p[@class='Ahead']|p[@class='Bhead']|p[@class='Chead']" mode="group"> 
     <xsl:variable name="this_att" select="substring-before(@class, 'head')"/> 
     <xsl:variable name="next" select="concat(translate($this_att, 'ABCDEFGH', 'BCDEFGHI'), 'head')"/> 
     <xsl:element name="section"> 
      <section-title><xsl:apply-templates/></section-title> 
      <xsl:for-each-group select="current-group() except ." group-starting-with="p[@class = $next]"> 
       <xsl:apply-templates select="." mode="group"/> 
      </xsl:for-each-group> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="p[not(ends-with(@class, 'head'))]" mode="group"> 
     <xsl:copy-of select="current-group()"/> 
    </xsl:template> 

</xsl:stylesheet> 

는 생산 :

<?xml version="1.0" encoding="UTF-8"?> 
<section> 
    <section-title>Heading 1</section-title> 
    <p class="txt">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p> 
    <section> 
     <section-title>Heading 1.1</section-title> 
     <p class="txt">Nibh euismod tincidunt ut laoreet dolore magna aliquam</p> 
     <p class="list">Investigationes demonstraverunt lectores legere</p> 
     <section> 
     <section-title>Heading 1.1.1</section-title> 
     <p class="txt">Typi non habent claritatem insitam</p> 
     </section> 
    </section> 
    <section> 
     <section-title>Heading 1.2</section-title> 
     <p class="txt">Lorem ipsum dolor sit amet</p> 
    </section> 
</section> 
+0

모자이크 XSL 논리, +1 –

+0

Saxon9he.jar 파일을 변환하지 않습니다. 동일한 모드 (mode = "group")는 두 개의 템플릿에서 사용됩니다. 그래서 우리가 뭘 할 수 있지? – Shanmugalakshmi

+0

Saxon-HE 9.4.0.3을 사용 중입니다. 경고없이 출력을 얻고 있습니다. 또한 http://xsltransform.net/bdxtq8에서 Saxon-HE 9.5.1.6을 사용해 보았으며 같은 결과를 얻었습니다. –