xslt - 无法解析链接目标

标签 xslt xsl-fo bookmarks apache-fop tableofcontents

我正在使用 Saxon (9.1) 和 FOP (0.95) 创建目录和书签树。除了以下 FOP 输出之外,一切正常:

WARNING: 1 link target could not be resolved and now point to the top of the page or is dysfunctional.

在生成的 PDF 中,所有链接(来自目录和书签树)都指向第一章。这是为什么?感谢您的帮助。

XML 输入文件:

<?xml version="1.0"?>
<document>
 <header>
  <title>This is the title of the document</title>
  <author>Mathias Mueller</author>
  <date>29/10/2013</date>
 </header>
 <body>
  <chapter level="1">
   <chaptitle>This is the title of the first chapter.</chaptitle>
   <p>All Saints Day (also known as All Hallows, Solemnity of All Saints...</p>      
  </chapter>
  <chapter level="2">
   <chaptitle>This is the title of the second chapter.</chaptitle>
   <p>In Western Christian theology, the day commemorates ...</p>
   </chapter>
   <chapter level="2">
    <chaptitle>This is the title of the third chapter.</chaptitle>
    <p>The feast of All Saints achieved great prominence in the ninth century, in the reign of the Byzantine Emperor, Leo VI the Wise...</p>
   </chapter>
  </body>
 <documentProperties>
   <orientation>portrait</orientation>
   <format>A4</format>
 </documentProperties>
</document>

XSLT 2.0-Stylesheet(仅省略定义 block 属性的属性集)

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

<xsl:template match="document">
 <xsl:element name="fo:root" xmlns="http://www.w3.org/1999/XSL/Format">
  <xsl:call-template name="docPr"/>
  <xsl:call-template name="bmt"/>
  <!--page sequence for TOC-->
  <xsl:element name="fo:page-sequence">
   <xsl:attribute name="master-reference">A4portrait</xsl:attribute>
   <xsl:element name="fo:flow">
    <xsl:attribute name="flow-name">xsl-region-body</xsl:attribute>
    <xsl:call-template name="toc"/>
   </xsl:element>
  </xsl:element>
  <!--rest-->
  <xsl:apply-templates/>
 </xsl:element>  
</xsl:template>

<xsl:template name="docPr">
 <xsl:element name="fo:layout-master-set">
  <xsl:if test="documentProperties/orientation eq 'portrait' and documentProperties/format eq 'A4'">
   <xsl:element name="fo:simple-page-master" use-attribute-sets="A4portrait">
    <xsl:element name="fo:region-body">
     <xsl:attribute name="margin-top">25mm</xsl:attribute>
     <xsl:attribute name="margin-bottom">20mm</xsl:attribute>
    </xsl:element>
   </xsl:element>
  </xsl:if>
 </xsl:element>
</xsl:template>

<xsl:template name="bmt">
 <xsl:element name="fo:bookmark-tree">
  <xsl:for-each select="body/chapter">
   <xsl:element name="fo:bookmark">
    <xsl:attribute name="internal-destination">{generate-id()}</xsl:attribute>
    <xsl:element name="fo:bookmark-title">
     <xsl:value-of select="chaptitle"/>
    </xsl:element>
   </xsl:element>
  </xsl:for-each>
 </xsl:element>
</xsl:template>

<xsl:template name="toc">
 <xsl:element name="fo:block">
  <xsl:attribute name="break-before">page</xsl:attribute>
   <xsl:element name="fo:block" use-attribute-sets="title2-block">
    <xsl:text>Table of Contents</xsl:text>
   </xsl:element>
   <xsl:for-each select="body/chapter">
    <xsl:element name="fo:block">
     <xsl:attribute name="text-align-last">justify</xsl:attribute>
     <xsl:element name="fo:basic-link">
      <xsl:attribute name="internal-destination">{generate-id()}</xsl:attribute>
      <xsl:value-of select="count(preceding::chapter) + 1"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="chaptitle"/>
      <xsl:element name="fo:leader">
       <xsl:attribute name="leader-pattern">dots</xsl:attribute>
      </xsl:element>
      <xsl:element name="fo:page-number-citation">
       <xsl:attribute name="ref-id">{generate-id()}</xsl:attribute>
      </xsl:element>
     </xsl:element>
    </xsl:element>
   </xsl:for-each>
  </xsl:element>
</xsl:template>


<xsl:template match="chapter">
 <xsl:element name="fo:page-sequence">
  <xsl:attribute name="master-reference">A4portrait</xsl:attribute>
  <xsl:attribute name="id">{generate-id()}</xsl:attribute>
  <xsl:element name="fo:flow">
   <xsl:attribute name="flow-name">xsl-region-body</xsl:attribute>
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:element>
</xsl:template>

<xsl:template match="chaptitle">
 <xsl:element name="fo:block" use-attribute-sets="title1-block">
  <xsl:value-of select="."/>
 </xsl:element>
</xsl:template>

<xsl:template match="p">
 <xsl:element name="fo:block" use-attribute-sets="normal-block">
  <xsl:value-of select="."/>
 </xsl:element>
</xsl:template>

最佳答案

我认为问题在于您调用 XSLT generate-id() 的方式功能。带括号的语法{}通常只能在 "" 内工作引号。现在发生的情况是所有 id 都被分配文字字符串 {generate-id()} 。我建议您将这些事件替换为 <xsl:value-of select="generate-id()"/>然后再试一次。

关于xslt - 无法解析链接目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19725073/

相关文章:

xslt - 需要帮助理解 - XPath/XSLT 中的上下文节点和当前节点

xml - 按属性排列的 XSLT 子级列表

vim - 设置 VIM 书签

phpstorm - 如何在 PHPStorm 中停靠书签工具窗口?

javascript - 使用 javascript 显示所有书签

java - 在 Java 中使用 XSLT 将 XML 转换为 HTML 字符串

xml - 如何检查元素是否存在并选择元素值的子字符串

xml - 遍历数组并根据优先级进行选择

anchor - FOP XSL-FO anchor 在外部目标中

java - 与 Xalan 一起使用 Saxon