XSL-FO 将标题与部分分组

XSL-FO Grouping title with Section

您好,我想将我的 xml 转换为分组标题

这是我的 xml 文件:

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

这是我的 xslt 文件:

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

预期输出为:

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

现在输出:

  Head1 Head 1
  1sub 1
  2sub 2

首先,您的 header 元素称为 header1,而不是 header。计算 header 个元素总是会给出意想不到的结果。

为了xsl:number多层次计数,您需要指定应该计数的元素,用|分隔。下面是生成 well-formed XSL-FO 文档的完整示例。

在您当前的输出中,文本过多。这是因为文本节点的 built-in 模板需要用空模板覆盖,匹配 text().

XSLT 样式表

<?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输出

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

渲染的 PDF 输出


在线尝试此解决方案 here and read up on numbering in XSLT, e.g. this excellent XML.com article