2013-12-10 31 views
1

我有一個目錄的XML。我從列表中爲XML中的每個錨點生成命名的.xhtml頁面。在項目的嵌套列表中選擇一個節點的屬性

目錄結構包含章節,其中還列出了章節中的小節。我正在試圖使用前一章錨點的@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 UI元素上去,然後拉出該元素的最近的前一個同級ref-chap

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

注意,由於ancestor::preceding-sibling::是反方向的軸,「最近」的匹配項(即最後一個按文檔順序)是[1]

+0

這真是太好了,正是我在找的東西,但沒有弄清楚。感謝您的好解釋。 – user2093335