【问题标题】:Xpath expression creating a TOC (html) from a xml sourceXpath 表达式从 xml 源创建 TOC (html)
【发布时间】:2014-06-01 08:14:31
【问题描述】:

出于学习目的,我正在为一个 docbook 文档开发一个轻量级的 XSLT。我的问题是生成目录 我猜我的 XPath 表达式是错误的,但我想不出正确的表达式。请在下面找到我的文件的 sn-ps。我使用 Saxon9.5 从 XML 翻译成 HTML。

样式表

<!-- Stylesheet -->
<xsl:template match='db:toc'>
    <div id='toc'>
        <h1>Table of contents</h1>
        <ol>
            <!-- This line is not working. -->
            <xsl:apply-templates select='db:chapter' mode='toc'/>
        </ol>
    </div>
</xsl:template>
<xsl:template match='db:chapter' mode='toc'>
    <li>
        <a href='{generate-id()}'>
            <xsl:value-of select='title'/>
        </a>
    </li>
</xsl:template>

文档来源

<!-- XML source -->
<book xmlns='http://docbook.org/ns/docbook' xml:lang="en">
    <toc/>
    <chapter>
        <title>Chapter 01</title>
        <para>
            Cupcake ipsum dolor sit amet candy. Muffin lemon drops soufflé croissant pastry jelly beans candy sweet 
            roll. 
        </para>

我的 HTML 输出

<!-- The HTML output -->
<div id="toc">
    <h1>Table of contents</h1>
    <ol></ol>
</div>

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    你是对的,这是这条线不起作用......

    <xsl:apply-templates select='db:chapter' mode='toc'/>
    

    原因是您当前处于与 toc 匹配的模板中。然而 chapter 不是 toc 的子元素,这是 apply-templates 试图选择的。它实际上是一个兄弟姐妹。试试这个

    <xsl:apply-templates select='following-sibling::db:chapter' mode='toc'/>
    

    或者可能是这个(获取 chapter 元素,它是 toc 的父元素的子元素)

    <xsl:apply-templates select='../db:chapter' mode='toc'/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多