创建动态书签

时间:2014-03-24 09:10:06

标签: xml xslt xsl-fo

我有xml,其中章节可以在xml中的任何级别:

<?xml>
  <chapter>
    <long-name>Chapter 1</long-name> <!-- 1-->
    <chapter>
      <long-name>Chapter A</long-name> <!-- 1.1 -->
       <chapter>
         <long-name>Chapter B</long-name> <!-- 1.1.1 -->
       </chapter>
    </chapter>
    <chapter>
         <long-name>Chapter C</long-name> <!-- 1.2-->
    </chapter>
  </chapter>
  <chapter>
         <long-name>Chapter 2</long-name> <!-- 2 -->
         <chapter>
            <long-name>Chapter D</long-name> <!-- 2.1-->
         </chapter>
         <chapter>
            <long-name>Chapter E</long-name> <!-- 2.2-->
         </chapter>
  </chapter>
</xml>

1. Chapter 1
   1.1 chapter A
     1.1.1 Chapter B
   1.2 Chapter C

  2.Chapter 2
    2.1 Chapter D
    2.2 Chapter E

我想在pdf输出中创建一个书签树。但我无法使用下面的代码。

<fo:bookmark-tree>
            <fo:bookmark internal-destination="TOC"
                starting-state="show">
                <fo:bookmark-title> Details </fo:bookmark-title>
                <xsl:for-each select="//CHAPTER">
                    <fo:bookmark internal-destination="CHAPTER/LONG-NAME"
                        starting-state="show">
                        <fo:bookmark-title>
                            <xsl:number format="1.1" count="CHAPTER" level="multiple" />
                            <xsl:value-of select="LONG-NAME" />                             
                        </fo:bookmark-title>
                    </fo:bookmark>
                </xsl:for-each>
            </fo:bookmark>

        </fo:bookmark-tree>

使用上面的xslt,我的所有章节都是在&#39;详细信息下创建的。同一级别的节点。

必需的XSLT OUTPUT:

<fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
     <fo:bookmark-title>1 Chapter 1</fo:bookmark-title>
         <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
            <fo:bookmark-title>1.1 Chapter A</fo:bookmark-title>

                <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
                    <fo:bookmark-title>1.1.1 Chapter B</fo:bookmark-title>
                </fo:bookmark>

        </fo:bookmark>
        <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
            <fo:bookmark-title>1.2 Chapter C</fo:bookmark-title>
        </fo:bookmark>
    </fo:bookmark>     

  <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
     <fo:bookmark-title>2 Chapter 2</fo:bookmark-title>

     <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
        <fo:bookmark-title>2.1 Chapter D</fo:bookmark-title>
    </fo:bookmark>

    <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
        <fo:bookmark-title>2.2 Chapter E</fo:bookmark-title>
    </fo:bookmark>
  </fo:bookmark>

请帮忙。

1 个答案:

答案 0 :(得分:2)

您应该删除count="CHAPTER"中的xsl:number。试试这个样式表

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>


<xsl:template match="/">
    <fo:bookmark-tree>
        <fo:bookmark internal-destination="TOC"
            starting-state="show">
            <xsl:apply-templates select="xml/chapter"/>
        </fo:bookmark>
    </fo:bookmark-tree>
</xsl:template>

<xsl:template match="chapter">
    <fo:bookmark internal-destination="CHAPTER/LONG-NAME"
        starting-state="show">
        <fo:bookmark-title>
            <xsl:number format="1.1 " level="multiple" />
            <xsl:value-of select="long-name" />                             
        </fo:bookmark-title>
        <xsl:apply-templates/>
    </fo:bookmark>
</xsl:template>

<xsl:template match="long-name"/>

</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="utf-8"?>
<fo:bookmark-tree xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:bookmark internal-destination="TOC" starting-state="show">
      <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
         <fo:bookmark-title>1 Chapter 1</fo:bookmark-title>
         <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
            <fo:bookmark-title>1.1 Chapter A</fo:bookmark-title>
            <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
               <fo:bookmark-title>1.1.1 Chapter B</fo:bookmark-title>
            </fo:bookmark>
         </fo:bookmark>
         <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
            <fo:bookmark-title>1.2 Chapter C</fo:bookmark-title>
         </fo:bookmark>
      </fo:bookmark>
      <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
         <fo:bookmark-title>2 Chapter 2</fo:bookmark-title>
         <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
            <fo:bookmark-title>2.1 Chapter D</fo:bookmark-title>
         </fo:bookmark>
         <fo:bookmark internal-destination="CHAPTER/LONG-NAME" starting-state="show">
            <fo:bookmark-title>2.2 Chapter E</fo:bookmark-title>
         </fo:bookmark>
      </fo:bookmark>
   </fo:bookmark>
</fo:bookmark-tree>