【问题标题】:XSL-FO Grouping title with SectionXSL-FO 用 Section 分组标题
【发布时间】:2015-04-20 11:36:54
【问题描述】:

您好,我想将我的 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

【问题讨论】:

  • 当前输出有什么问题?
  • 嗨,我只得到以下信息:Head 1 1 sub 1 2 sub 2

标签: xml xslt xslt-2.0 xsl-fo


【解决方案1】:

首先,您的标题元素称为header1,而不是header。计数header 元素总是会产生意想不到的结果。

要使xsl:number 计数多个级别,您需要指定应计数的元素,并用| 分隔它们。下面是一个生成格式良好的 XSL-FO 文档的完整示例。

在您当前的输出中,文本过多。这是因为文本节点的内置模板需要用空模板覆盖,匹配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 并阅读 XSLT 中的编号,例如this excellent XML.com article.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2015-06-14
    相关资源
    最近更新 更多