XSL-FO Grouping title with Section

2019-08-14 07:06发布

问题:

Hello I want to transform my xml to grouping my titles

here is my xml file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <header1>
        <title>Head 1</title>
        <sub>
            <title>sub 1</title>
        </sub>
        <sub>
            <title>sub 2</title>
        </sub>

    </header1>
</root>

here is my xslt file:

  <xsl:template match="header1">     
        <fo:block>      
            <xsl:number level="multiple" count="header" format="1"/>  
            <xsl:value-of select="./title/text()"/>  
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>

    <xsl:template match="sub">
        <fo:block>      
            <xsl:number level="multiple" count="sub" format="1.1"/>  
            <xsl:value-of select="./title/text()"/>      
        </fo:block>
    </xsl:template>

The expected output is:

1 Head 
1.1 Head - sub 1
1.2 Head - sub 2

The output now:

  Head1 Head 1
  1sub 1
  2sub 2

回答1:

First of all, your header element is called header1, not header. Counting header elements will always give unexpected results.

For xsl:number to count on multiple levels, you need to specify the elements that should be counted, separating them with |. Below is a full example that generates a well-formed XSL-FO document.

In your current output, there is too much text. That's because of the built-in template for text nodes that you need to override with an empty template, matching text().

XSLT Stylesheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
   xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="/root">
      <fo:root>
        <fo:layout-master-set>
          <fo:simple-page-master master-name="page"
            page-height="297mm" page-width="210mm"
            margin-top="20mm" margin-bottom="10mm"
            margin-left="25mm" margin-right="25mm">
            <fo:region-body
              margin-top="0mm" margin-bottom="15mm"
              margin-left="0mm" margin-right="0mm"/>
            <fo:region-after extent="10mm"/>
          </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="page">
          <fo:flow flow-name="xsl-region-body">
            <xsl:apply-templates/>
          </fo:flow>
        </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template match="header1">     
        <fo:block>      
            <xsl:number level="multiple" count="header1" format="1 "/>  
            <xsl:value-of select="title"/> 
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>

    <xsl:template match="sub">
        <fo:block>      
            <xsl:number level="multiple" count="sub|header1" format="1. "/>  
            <xsl:value-of select="concat('Head - ',title)"/>      
        </fo:block>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:transform>

XSL-FO Output

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="page"
                             page-height="297mm"
                             page-width="210mm"
                             margin-top="20mm"
                             margin-bottom="10mm"
                             margin-left="25mm"
                             margin-right="25mm">
         <fo:region-body margin-top="0mm"
                         margin-bottom="15mm"
                         margin-left="0mm"
                         margin-right="0mm"/>
         <fo:region-after extent="10mm"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="page">
      <fo:flow flow-name="xsl-region-body">
         <fo:block>1 Head 1<fo:block>1.1. Head - sub 1</fo:block>
            <fo:block>1.2. Head - sub 2</fo:block>
         </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

Rendered PDF Output


Try this solution online here and read up on numbering in XSLT, e.g. this excellent XML.com article.