2016-04-26 135 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> 
相关问题