Вопрос

I have the xml where chapters can be at any level in the 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

I want to create a bookmark tree in my pdf output. But I cannot get it working with the below code.

<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>

With the above xslt, all my chapters are getting created under 'Details' node at the same level.

Required 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>

Please help.

Это было полезно?

Решение

you should delete count="CHAPTER" in xsl:number. Try this stylesheet

<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>

it outputs:

<?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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top