XSL-FO 分组标题与节 [英] XSL-FO Grouping title with Section

查看:37
本文介绍了XSL-FO 分组标题与节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I want to transform my xml to grouping my titles

这是我的 xml 文件:

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>

这是我的 xslt 文件:

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>

预期输出为:

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

现在的输出:

  Head1 Head 1
  1sub 1
  2sub 2

推荐答案

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

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

对于xsl:number要在多个级别进行计数,您需要指定应该计数的元素,用|分隔它们.下面是生成格式良好的 XSL-FO 文档的完整示例.

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.

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

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 样式表

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

<小时>

在线试用此解决方案此处并阅读 XSLT 中的编号,例如这篇优秀的 XML.com 文章.


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

这篇关于XSL-FO 分组标题与节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆