2013-02-07 2 views
0

, 나는 제목과 챕터를하고 난되도록 그것의 내용의 테이블을 생성해야합니다XSLT에서 노드를 두 번 사용하는 방법은 무엇입니까? 내 XML에서

<chapter id="1"><title>Chapter 1</title><p>text</p></chapter> 
<chapter id="2"><title>Chapter 2</title><p>text</p></chapter> 

불행하게도
<!-- Table Of Contents --> 
<div class="contents"> 
    <ul> 
    <li><a href="#1">Chapter 1</a></li> 
    <li><a href="#2">Chapter 2</a></li> 
    </ul> 
</div> 

<!-- Actual Content --> 
<div class="chapter" id="1"><p>text</p></div> 
<div class="chapter" id="2"><p>text</p></div> 

로 변환 내가 xsl:for-each를 사용하려고하면, 목차를 생성하기 위해 실제 챕터가 출력에서 ​​사라지는 것처럼 보입니다. 이 문제를 어떻게 해결할 수 있습니까?

+0

이 예제에서는 현재 사용중인 XSLT를 보여 주시겠습니까? 감사! –

답변

6

이 XSLT :

<chapters> 
    <chapter id="1"> 
    <title>Chapter 1</title> 
    <p>text</p> 
    </chapter> 
    <chapter id="2"> 
    <title>Chapter 2</title> 
    <p>text</p> 
    </chapter> 
</chapters> 

생산됩니다 : 당신이 우리에게 당신의 XSLT를 보여줄 수 있다면

<div> 
    <div class="contents"> 
    <ul> 
     <li> 
     <a href="#1">Chapter 1</a> 
     </li> 
     <li> 
     <a href="#2">Chapter 2</a> 
     </li> 
    </ul> 
    </div> 
    <div class="chapter" id="1"> 
    <p>text</p> 
    </div> 
    <div class="chapter" id="2"> 
    <p>text</p> 
    </div> 
</div> 

(내가와 있다는 생각이 입력에 적용

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

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

    <xsl:template match="/*"> 
    <div> 
     <div class="contents"> 
     <ul> 
      <xsl:apply-templates select="chapter" mode="contents" /> 
     </ul> 
     </div> 

     <xsl:apply-templates select="chapter" /> 
    </div> 
    </xsl:template> 

    <xsl:template match="chapter" mode="contents"> 
    <li> 
     <a href="#{@id}"> 
     <xsl:value-of select="title" /> 
     </a> 
    </li> 
    </xsl:template> 

    <xsl:template match="chapter"> 
    <div class="chapter" id="{@id}"> 
     <xsl:apply-templates select="*" /> 
    </div> 
    </xsl:template> 

    <xsl:template match="chapter/title" /> 
</xsl:stylesheet> 

당신에게 틀림없이 당신이 무엇을하고 있는지 말할 수있는 명성 점수 2,410 점).

+0

완벽! +1 .... –

+0

고마워, 나는 아직 모드를 사용하는 데 익숙하지 않았고이 상황에서 모드를 적용하는 방법을 이해하지 못했다. – snitko

관련 문제