2016-04-26 133 views
0

輸入:XSLT:具有特定屬性值的XPath選擇元素

<list list-type="simple" specific-use="front"> 
    <list-item><p>Preface <xref rid="b-9781783084944-FM-001" ref-type="sec">00</xref></p></list-item> 
    <list-item><p>Series Title <xref rid="b-9781783084944-FM-003" ref-type="sec">00</xref></p></list-item> 
    <list-item><p>Dedication</p></list-item> 
    <list-item><p>Acknowledgments <xref rid="b-9781783084944-FM-005" ref-type="sec">00</xref></p></list-item> 
    <list-item><p>Contributors <xref rid="b-9781783084944-FM-006" ref-type="sec">00</xref></p></list-item> 
    <list-item><p>Glossary <xref rid="b-9781783084944-FM-008" ref-type="sec">00</xref></p></list-item> 
</list> 

我需要以下

<div class="pagebreak" id="b-9781783084944-FM-002"> 
    <h2 class="PET"><a href="#tocb-9781783084944-FM-002">CONTENTS</a></h2> 
    <div class="TocPrelims"><a href="#b-9781783084944-FM-001">Preface</a></div> 
    <div class="TocPrelims"><a href="#b-9781783084944-FM-002">Series Title</a> </div> 
</div> 

輸出LIK我的XSLT:

<xsl:template match="list[@specific-use='front'][@list-type='simple']/list-item/p"> 
    <div class="TocPrelims"> 
    <a> 
     <xsl:attribute name="href"> 
      <xsl:text>#toc</xsl:text> 
     <xsl:copy-of select="//list[@specific-use='front'][@list-type='simple']/list-item/p/xref[@rid]"/> 
     </xsl:attribute> 
     <xsl:apply-templates/> 
    </a> 
    </div> 
</xsl:template> 

以上礦井的編碼不正確請給點建議。

+1

你能否改善你問題中代碼的格式?如果您編輯問題並突出顯示代碼示例,那麼只需單擊「{}」按鈕對其進行格式化(在每行之前放置4個空格)以使其可讀。謝謝! –

+0

@Raja Ananth,如果下面的答案適合你的要求,那麼接受tic正確的標記。 –

回答

1

你有一個問題,這條線:

<xsl:copy-of select="//list[@specific-use='front'][@list-type='simple']/list-item/p/xref[@rid]"/> 

首先,病情會選擇所有xref元素,但你只需要一個給你定位在當前p。其次,如果xref屬性具有rid屬性,但實際上要選擇rid屬性,則選擇xref元素。你也真的想用xsl:value-of這裏

<xsl:value-of select="xref/@rid"/> 

試試這個模版。

<xsl:template match="list[@specific-use='front'][@list-type='simple']/list-item/p"> 
    <div class="TocPrelims"> 
     <a> 
     <xsl:attribute name="href"> 
      <xsl:text>#toc</xsl:text> 
      <xsl:value-of select="xref/@rid"/> 
     </xsl:attribute> 
     <xsl:value-of select="text()[1]" /> 
     </a> 
    </div> 
</xsl:template> 

事實上,你可以使用屬性值模板的把它簡化爲這樣:

<xsl:template match="list[@specific-use='front'][@list-type='simple']/list-item/p"> 
    <div class="TocPrelims"> 
     <a href="#toc{xref/@rid}"> 
     <xsl:value-of select="text()[1]" /> 
     </a> 
    </div> 
</xsl:template> 
相關問題