2013-12-10 6 views
1

XML 목차가 있습니다. 각 XML 앵커 지점 목록에서 명명 된 .xhtml 페이지를 생성하고 있습니다.중첩 된 항목 목록의 노드 속성 선택

TOC의 구조에는 해당 장의 하위 절이 나열되어 있습니다. 이전 챕터 앵커의 @href 값을 사용하여 해당 하위 섹션 앵커 이름을 동적으로 지정하려고합니다.

샘플 XML :

<div class="toc" id="s9781483331812.i34"><a class="page" id="pbr-v" title="v"/> 
<p class="toc-title">Contents</p> 
<ul class="toc"> 
     <li class="toc-item"> 
      <a class="ref-chap" href="#s132" id="s35"><b>Preface</b></a> 
      <a class="page-ref" href="#pbr-vii" id="s36"><b>vii</b></a> 
     </li> 
     <li class="toc-item"> 
      <a class="ref-chap" href="#s135" id="s37"><b>Introduction</b></a> 
      <a class="page-ref" href="#pbr-xi" id="s38"><b>xi</b></a> 
     </li> 
     <li class="toc-item"> 
      <span class="toc-label" title="1"><b>1.</b></span> 
      <a class="ref-chap" href="#s147" id="s39"><b>Chapter</b></a>   
      <a class="page-ref" href="#pbr-1" id="s40"><b>1</b></a> 
      <ul class="chapter-section"> 
       <li class="toc-item">  
        <a class="ref-chap" href="#s152" id="s41">Subsection 1</a>   
        <a class="page-ref" href="#pbr-2" id="s42">2</a> 
       </li> 
       <li class="toc-item">  
        <a class="ref-chap" href="#s158" id="s43">Subsection 2</a>   
        <a class="page-ref" href="#pbr-6" id="s44">6</a> 
       </li> 
       <li class="toc-item">  
        <a class="ref-chap" href="#s159" id="s45">Subsection 3</a>   
        <a class="page-ref" href="#pbr-10" id="s46">10</a> 
       </li> 
      </ul> 
     </li> 
    </ul> 

샘플 XSLT :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
exclude-result-prefixes="xs" 
version="2.0"> 


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

<xsl:template match="div[@class='toc']//li[@class='toc-item']"> 
    <xsl:element name="li"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="a[@class='ref-chap']"> 
    <xsl:element name="a"> 
     <xsl:attribute name="href" select="concat(substring-after(@href, '#'),'.xhtml')"/> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

<xsl:template priority="1" match="ul[@class='chapter-section']//a[@class='ref-chap']"> 
    <xsl:variable name="pagenumber" select="following-sibling::a[@class='page-ref']"/> 
    <xsl:element name="a"> 
     <xsl:attribute name="href" select="concat(substring-after(ancestor::ul[@class='toc']//li[@class='toc-item']//a[@class='ref-chap'][last()]/@href, '#'),'.xhtml','#page',$pagenumber)"/> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

내 원하는 출력은 :

<div class="toc" id="s9781483331812.i34"> 
<a class="page" id="pbr-v" title="v"/> 
<p class="toc-title">Contents</p> 
<ul class="toc"> 
     <li> 
      <a href="s132.xhtml"><b>Preface</b></a> 
      <a class="page-ref" href="#pbr-vii" id="s36"><b>vii</b></a> 
     </li> 
     <li> 
      <a href="s135.xhtml"><b>Introduction</b></a> 
      <a class="page-ref" href="#pbr-xi" id="s38"><b>xi</b></a> 
     </li> 
     <li> 
      <span class="toc-label" title="1"><b>1.</b></span> 
      <a href="s147.xhtml"><b>Chapter</b></a>   
      <a class="page-ref" href="#pbr-1" id="s40"><b>1</b></a> 
      <ul class="chapter-section"> 
       <li>  
        <a href="s147.xhtml#page2">Subsection 1</a>   
        <a class="page-ref" href="#pbr-2" id="s42">2</a> 
       </li> 
       <li>  
        <a href="s147.xhtml#page6">Subsection 2</a>   
        <a class="page-ref" href="#pbr-6" id="s44">6</a> 
       </li> 
       <li>  
        <a href="s147.xhtml#page10">Subsection 3</a>   
        <a class="page-ref" href="#pbr-10" id="s46">10</a> 
       </li> 
      </ul> 
     </li> 
    </ul> 

"하나 이상의 항목 시퀀스가 ​​substring-after() ("# s132 ","# s135 ", ...)의 첫 번째 인수로 허용되지 않습니다. 분명히 XPath는 하나가 아닌 여러 값을 선택합니다. 그러나, 나는 이것을 해결하는 방법을 알아낼 수 없습니다.

참고 : 장 서브 섹션 수는 알려져 있지 않으며 다를 수 있습니다.

답변

1

귀하의 XPath 식

ancestor::ul[@class='toc']//li[@class='toc-item']//a[@class='ref-chap'][last()]/@href 

확실히 너무 많이 선택할 것 - 그것은 TOC 내의 모든 toc-item 목록 항목 내부의 모든 ref-chap 앵커 보면, 당신에게 모든 앵커로 구성된 시퀀스를 줄 것이다 그들의 각각의 부모님 안에 마지막으로 ref-chap입니다. toc-item에는 하나 이상의 ref-chap이 포함되어 있으므로 모두을 의미합니다.

특정 ref-chap의 맥락에서 현재 감안할 때, 당신은 모든 방법 toc 수준까지 갈 필요가 없습니다 만 빼 후까지 가장 가까운 chapter-section UL 요소로 가서 필요 해당 요소의 가장 가까운 ref-chap 형제 이전 : ancestor::preceding-sibling::는 반대 방향으로 축이기 때문에

ancestor::ul[@class='chapter-section'][1]/preceding-sibling::a[@class='ref-chap'][1]/@href 

은 참고 "가장 가까운"일치하는 항목 (문서 순서 즉, 마지막은) [1]입니다.

+0

이것은 내가 찾고있는 정확하게 위대하다. 그러나 밖으로 계산하는 것은 실패한다. 좋은 설명을 주셔서 감사합니다. – user2093335