2014-06-22 3 views
1

나는 그런 메뉴 바 메뉴를 만들고 싶었 :XSLT는

<table width="100%"> 
       <tr> 
        <td id="navBar" width="15%"> 
         <nav> 
          <ul> 
           <li><b id="menu">Menu</b></li> 
           <!-- menu here --> 
          </ul> 
         </nav> 
        </td> 
        <td width="85%"> 
         <xsl:apply-templates select="frontmatter"/> 
        </td> 
       </tr> 
      </table> 

을 그리고 난 그 구조와 XML 파일이 : 기본적으로

<body> 
    <chapter id="...1"> 
       <title>...</title> 
       <section id="...1"> 
           <title>...</title> 
           <subsection id="...1"> 
               <title>...</title> 
               <subsubsection id="...1"> 
       <section id="...2"> 
        . 
        . 
    <chapter id="...2"> 
     . 
     . 

, 난 많이 가질 수 있습니다 chapterchaptertitle입니다.

section는 제목과 section과 다양성과 같은 방법이 않는 각 sectiontitle하고, subsectionsubsubsection와 여러 section 수 있습니다.

내 질문은 : 주문을 매우 중요하게 할 때 제목을 얻고 내비게이션 바를 <a href ...>으로 어떻게 얻을 수 있습니까? 예를 들어 (또한 올바른 번호로) :

1. title (referencing to chapter 1) 
1.1 title of section (referecing for chapter 1 section 1) 
etc etc 

난 내가 XSLT와 그렇게 할 수있는 방법을 모르겠어요.

답변

0

<xsl:number> 요소를 사용하여 번호 매기기를 생성 할 수 있습니다. count 특성을 사용하여 계산할 요소를 포함하고 level="multiple"을 여러 중첩 수준에서 계산합니다. 각 title 인쇄하고 <value-of> 전에

<xsl:number format="1. " level="multiple" count="chapter | section | subsection | subsubsection"/> 

, 당신은 멀티 섹션 번호를 생성 할 수 있습니다 다음 format는 포함시킴으로써 등

아랍어 숫자, 로마 숫자를 증가 할 수 있습니다.

다음은 스타일 시트입니다. id 속성의 데이터는 사용하지 않지만 (당연히 할 수는 있지만) 양식 번호가 #sec1.1.1 인 섹션을 사용하여 URI 조각을 생성합니다. 각 항목은 명명 된 템플릿으로 생성됩니다.

<table width="100%"> 
    <tr> 
     <td id="navBar" width="15%"> 
     <nav> 
      <ul> 
       <li> 
        <b id="menu">Menu</b> 
       </li> 
       <li> 
        <a href="#sec1">1. Title of chapter 1</a> 
       </li> 
       <li> 
        <a href="#sec1.1">1.1. Title of Section</a> 
       </li> 
       <li> 
        <a href="#sec1.1.1">1.1.1. Title of SubSection</a> 
       </li> 
       <li> 
        <a href="#sec1.1.1.1">1.1.1.1. Title of SubSubSection</a> 
       </li> 
       <li> 
        <a href="#sec1.2">1.2. Title of Section</a> 
       </li> 
       <li> 
        <a href="#sec2">2. Title of Chapter 2</a> 
       </li> 
       <li> 
        <a href="#sec2.1">2.1. Title of Section</a> 
       </li> 
       <li> 
        <a href="#sec2.1.1">2.1.1. Title of SubSection</a> 
       </li> 
       <li> 
        <a href="#sec2.1.1.1">2.1.1.1. Title of SubSubSection</a> 
       </li> 
       <li> 
        <a href="#sec2.2">2.2. Title of Section</a> 
       </li> 
      </ul> 
     </nav> 
     </td> 
     <td width="85%"/> 
    </tr> 
</table> 

당신은 그것을 밖으로 시도 할 수 있습니다 : 당신은이 결과를해야합니다

<body> 
    <chapter id="c1"> 
     <title>Title of chapter 1</title> 
     <section id="c1s1"> 
      <title>Title of Section</title> 
      <subsection id="s1s1"> 
       <title>Title of SubSection</title> 
       <subsubsection id="s1s1s1"> 
        <title>Title of SubSubSection</title> 
       </subsubsection> 
      </subsection> 
     </section> 
     <section id="c1s2"> 
      <title>Title of Section</title> 
      <section id="c1s2s1"> 
       <title>Title of SubSection</title> 
       <subsection id="c1s2s1s1"> 
        <title>Title of SubSubSection</title> 
       </subsection> 
      </section> 
     </section> 
    </chapter> 
    <chapter id="c2"> 
     <title>Title of Chapter 2</title> 
     <section id="c2s1"> 
      <title>Title of Section</title> 
      <subsection id="c2s1s1"> 
       <title>Title of SubSection</title> 
       <subsubsection id="c2s1s1s1"> 
        <title>Title of SubSubSection</title> 
       </subsubsection> 
      </subsection> 
     </section> 
     <section id="c2s2"> 
      <title>Title of Section</title> 
      <section id="c2s2s1"> 
       <title>Title of SubSection</title> 
       <subsection id="c2s2s1s1"> 
        <title>Title of SubSubSection</title> 
       </subsection> 
      </section> 
     </section> 
    </chapter> 
</body> 

:이 입력을 사용

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output indent="yes"/> 
    <xsl:template match="/"> 
     <table width="100%"> 
      <tr> 
       <td id="navBar" width="15%"> 
        <nav> 
         <ul> 
          <li><b id="menu">Menu</b></li> 
          <xsl:apply-templates select="body/chapter"/> 
         </ul> 
        </nav> 
       </td> 
       <td width="85%"> 
        <xsl:apply-templates select="frontmatter"/> 
       </td> 
      </tr> 
     </table> 
    </xsl:template> 

    <xsl:template match="chapter"> 
     <xsl:call-template name="generate-item"/> 
     <xsl:apply-templates select="section"/> 
    </xsl:template> 

    <xsl:template match="section"> 
     <xsl:call-template name="generate-item"/> 
     <xsl:apply-templates select="subsection"/> 
    </xsl:template> 

    <xsl:template match="subsection"> 
     <xsl:call-template name="generate-item"/> 
     <xsl:apply-templates select="subsubsection"/> 
    </xsl:template> 

    <xsl:template match="subsubsection"> 
     <xsl:call-template name="generate-item"/> 
    </xsl:template> 

    <xsl:template name="generate-item"> 
     <li><a> 
      <xsl:attribute name="href"> 
       <xsl:text>#sec</xsl:text> 
       <xsl:number format="1" level="multiple" count="chapter | section | subsection | subsubsection"/> 
      </xsl:attribute> 
      <xsl:number format="1. " level="multiple" count="chapter | section | subsection | subsubsection"/><xsl:value-of select="title"/> 
     </a></li> 
    </xsl:template> 

</xsl:stylesheet> 

: 순서는 중첩 된 부분을 호출하는 각 템플릿에 의해 제어됩니다 이 XSLT Fiddle

+0

작동합니다! 그러나 나는 또 다른 질문을 가지고 있습니다 : 만약 그 정보를 몸에 담아 앵커를 만들기를 원한다면 가능합니까? – Damiii

+1

예. 같은 요소에서 데이터를 추출하는 새 템플릿이 필요할 것입니다. 동일한 소스 요소와 일치하는 템플릿을 가질 수 있으므로'mode' 속성을 사용하여 두 작업을 구별 할 수 있습니다. 아마도'mode = "toc"'와'mode = "body"'일 것입니다. – helderdarocha

+0

나는 그것을 만들었거나, 매개 변수를 true 또는 false로 전달했다고 생각합니다! :) 헬러 감사합니다! – Damiii