|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
引言
XSL FO(Extensible Stylesheet Language Formatting Objects)是一种强大的标记语言,专门用于格式化XML数据,特别适合创建高质量的打印文档和PDF文件。在专业文档制作过程中,索引是一个至关重要的组成部分,它不仅能显著提升信息检索效率,还能大大改善用户体验。本文将全面介绍如何使用XSL FO创建专业级的文档索引,从基础概念到高级应用技巧,帮助读者掌握这一强大工具,提升文档的专业性和可用性。
XSL FO基础
什么是XSL FO?
XSL FO是W3C推荐的一种标准,用于描述文档的格式和布局。它是XSL(Extensible Stylesheet Language)的一部分,专门负责文档的呈现层面。与HTML或CSS不同,XSL FO更加关注打印媒体和分页文档的精确控制。
XSL FO文档结构
一个基本的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:layout-master-set>
-
- <fo:page-sequence master-reference="main">
- <fo:flow flow-name="xsl-region-body">
- <!-- 文档内容 -->
- </fo:flow>
- </fo:page-sequence>
- </fo:root>
复制代码
XSL FO处理流程
XSL FO文档通常通过以下流程生成:
1. 创建源XML文档
2. 编写XSLT样式表将XML转换为XSL FO
3. 使用XSL FO处理器(如Apache FOP或RenderX)将XSL FO转换为PDF或其他格式
索引的基本概念
什么是索引?
索引是文档末尾的一个有序列表,包含重要术语、概念和它们在文档中出现的位置。良好的索引应该:
• 全面:覆盖文档中的重要内容
• 精确:准确指向相关内容
• 一致:使用统一的术语和格式
• 用户友好:考虑到用户的检索习惯
索引的类型
常见的索引类型包括:
1. 主题索引:按主题或概念组织
2. 作者索引:按作者姓名组织
3. 图表索引:列出所有图表及其位置
4. 法律文档索引:按法规、案例等组织
使用XSL FO创建简单索引
准备工作
在创建索引之前,我们需要:
1. 在源XML文档中标记索引条目
2. 创建XSLT样式表提取这些条目
3. 在XSL FO中格式化索引
标记索引条目
首先,在源XML文档中添加索引标记:
- <book>
- <chapter>
- <title>Introduction</title>
- <para>This is about <index-term term="XSL FO"/>XSL FO formatting.</para>
- <para>More text about <index-term term="Formatting Objects"/>Formatting Objects.</para>
- </chapter>
- </book>
复制代码
创建基本索引XSLT
接下来,创建XSLT样式表来提取和格式化索引:
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:fo="http://www.w3.org/1999/XSL/Format">
-
- <!-- 提取所有索引条目 -->
- <xsl:key name="index-terms" match="index-term" use="@term"/>
-
- <!-- 主模板 -->
- <xsl:template match="/">
- <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
- <fo:layout-master-set>
- <fo:simple-page-master master-name="main" page-height="29.7cm" page-width="21cm">
- <fo:region-body margin="2cm"/>
- </fo:simple-page-master>
- </fo:layout-master-set>
-
- <fo:page-sequence master-reference="main">
- <fo:flow flow-name="xsl-region-body">
- <!-- 处理文档内容 -->
- <xsl:apply-templates select="book"/>
-
- <!-- 生成索引 -->
- <fo:block break-before="page" font-size="18pt" font-weight="bold" margin-bottom="12pt">
- Index
- </fo:block>
- <xsl:call-template name="generate-index"/>
- </fo:flow>
- </fo:page-sequence>
- </fo:root>
- </xsl:template>
-
- <!-- 生成索引的模板 -->
- <xsl:template name="generate-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="6pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
-
- <!-- 处理其他元素 -->
- <xsl:template match="chapter">
- <fo:block break-before="page" font-size="16pt" font-weight="bold" margin-bottom="10pt">
- <xsl:value-of select="title"/>
- </fo:block>
- <xsl:apply-templates select="para"/>
- </xsl:template>
-
- <xsl:template match="para">
- <fo:block margin-bottom="8pt">
- <xsl:apply-templates/>
- </fo:block>
- </xsl:template>
-
- <xsl:template match="index-term">
- <!-- 索引术语在正文中不显示 -->
- <xsl:apply-templates/>
- </xsl:template>
- </xsl:stylesheet>
复制代码
这个基本示例会创建一个简单的索引,列出所有标记的术语及其出现的章节。但我们可以进一步改进和完善它。
索引的高级功能
分组索引条目
在专业索引中,通常需要将相关术语分组。我们可以通过修改XSLT来实现这一点:
- <xsl:template name="generate-index">
- <!-- 按首字母分组 -->
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <xsl:variable name="current-letter" select="substring-upper(@term, 1, 1)"/>
- <xsl:if test="not(preceding-sibling::index-term[substring-upper(@term, 1, 1) = $current-letter])">
- <fo:block font-weight="bold" margin-top="12pt" margin-bottom="6pt">
- <xsl:value-of select="$current-letter"/>
- </fo:block>
- </xsl:if>
-
- <fo:block margin-left="12pt" margin-bottom="6pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
复制代码
多级索引
对于复杂文档,可能需要多级索引:
- <!-- 在源XML中添加多级索引标记 -->
- <para>This is about <index-term term="XSL FO" subterm="basic concepts"/>XSL FO basic concepts.</para>
- <para>More text about <index-term term="XSL FO" subterm="advanced features"/>XSL FO advanced features.</para>
- <!-- 修改XSLT以处理多级索引 -->
- <xsl:key name="index-terms" match="index-term" use="@term"/>
- <xsl:key name="subterms" match="index-term" use="concat(@term, '::', @subterm)"/>
- <xsl:template name="generate-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <xsl:variable name="current-term" select="@term"/>
-
- <!-- 主术语 -->
- <fo:block margin-bottom="6pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)[not(@subterm)]">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
-
- <!-- 子术语 -->
- <xsl:for-each select="//index-term[@term = $current-term and @subterm][generate-id() = generate-id(key('subterms', concat(@term, '::', @subterm))[1])]">
- <xsl:sort select="@subterm"/>
- <fo:block margin-left="12pt" margin-bottom="6pt">
- <xsl:value-of select="@subterm"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('subterms', concat(@term, '::', @subterm))">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
- </xsl:for-each>
- </xsl:for-each>
- </xsl:template>
复制代码
交叉引用
索引中的交叉引用可以帮助用户找到相关术语:
- <!-- 在源XML中添加交叉引用 -->
- <index-term term="Formatting Objects" see="XSL FO"/>
- <!-- 修改XSLT以处理交叉引用 -->
- <xsl:template name="generate-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="6pt">
- <xsl:value-of select="@term"/>
- <xsl:choose>
- <xsl:when test="@see">
- <xsl:text> see </xsl:text>
- <xsl:value-of select="@see"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </xsl:otherwise>
- </xsl:choose>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
复制代码
页码范围
当术语在连续多页出现时,使用页码范围更为简洁:
- <xsl:template name="generate-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="6pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
-
- <!-- 获取并排序页码 -->
- <xsl:variable name="pages">
- <xsl:for-each select="key('index-terms', @term)">
- <page>
- <xsl:value-of select="ancestor::chapter/title"/>
- </page>
- </xsl:for-each>
- </xsl:variable>
-
- <!-- 输出页码或范围 -->
- <xsl:call-template name="format-page-ranges">
- <xsl:with-param name="pages" select="$pages"/>
- </xsl:call-template>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
- <xsl:template name="format-page-ranges">
- <xsl:param name="pages"/>
- <xsl:param name="position" select="1"/>
-
- <xsl:if test="$position <= count($pages/page)">
- <xsl:variable name="current" select="$pages/page[$position]"/>
- <xsl:variable name="next" select="$pages/page[$position + 1]"/>
-
- <xsl:choose>
- <xsl:when test="$next and $next = $current + 1">
- <!-- 开始一个范围 -->
- <xsl:value-of select="$current"/>
- <xsl:text>–</xsl:text>
-
- <!-- 找到范围的结束 -->
- <xsl:call-template name="find-range-end">
- <xsl:with-param name="pages" select="$pages"/>
- <xsl:with-param name="start" select="$position + 1"/>
- <xsl:with-param name="expected" select="$current + 2"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="$current"/>
- <xsl:if test="$position < count($pages/page)">
- <xsl:text>, </xsl:text>
- </xsl:if>
- </xsl:otherwise>
- </xsl:choose>
-
- <!-- 处理下一页 -->
- <xsl:call-template name="format-page-ranges">
- <xsl:with-param name="pages" select="$pages"/>
- <xsl:with-param name="position" select="$position + 1"/>
- </xsl:call-template>
- </xsl:if>
- </xsl:template>
- <xsl:template name="find-range-end">
- <xsl:param name="pages"/>
- <xsl:param name="start"/>
- <xsl:param name="expected"/>
-
- <xsl:choose>
- <xsl:when test="$start <= count($pages/page) and $pages/page[$start] = $expected">
- <!-- 继续查找 -->
- <xsl:call-template name="find-range-end">
- <xsl:with-param name="pages" select="$pages"/>
- <xsl:with-param name="start" select="$start + 1"/>
- <xsl:with-param name="expected" select="$expected + 1"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <!-- 输出范围结束 -->
- <xsl:value-of select="$pages/page[$start - 1]"/>
- <xsl:if test="$start <= count($pages/page)">
- <xsl:text>, </xsl:text>
- </xsl:if>
-
- <!-- 继续处理剩余页码 -->
- <xsl:call-template name="format-page-ranges">
- <xsl:with-param name="pages" select="$pages"/>
- <xsl:with-param name="position" select="$start"/>
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
复制代码
索引样式定制
字体和间距
自定义索引的字体和间距可以提升可读性:
- <xsl:template name="generate-index">
- <fo:block font-family="Arial, sans-serif" font-size="10pt">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt" text-indent="-12pt" start-indent="12pt">
- <fo:inline font-weight="bold">
- <xsl:value-of select="@term"/>
- </fo:inline>
- <fo:inline>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:inline>
- </fo:block>
- </xsl:for-each>
- </fo:block>
- </xsl:template>
复制代码
分栏布局
对于大型索引,使用分栏布局可以节省空间并提高可读性:
- <fo:flow flow-name="xsl-region-body">
- <!-- 处理文档内容 -->
- <xsl:apply-templates select="book"/>
-
- <!-- 生成索引 -->
- <fo:block break-before="page" font-size="18pt" font-weight="bold" margin-bottom="12pt">
- Index
- </fo:block>
-
- <fo:block-container>
- <fo:block font-family="Arial, sans-serif" font-size="10pt">
- <fo:table table-layout="fixed" width="100%">
- <fo:table-column column-width="50%"/>
- <fo:table-column column-width="50%"/>
- <fo:table-body>
- <fo:table-row>
- <fo:table-cell>
- <fo:block>
- <!-- 第一列索引 -->
- <xsl:call-template name="generate-index-column">
- <xsl:with-param name="terms" select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])][position() mod 2 = 1]"/>
- </xsl:call-template>
- </fo:block>
- </fo:table-cell>
- <fo:table-cell>
- <fo:block>
- <!-- 第二列索引 -->
- <xsl:call-template name="generate-index-column">
- <xsl:with-param name="terms" select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])][position() mod 2 = 0]"/>
- </xsl:call-template>
- </fo:block>
- </fo:table-cell>
- </fo:table-row>
- </fo:table-body>
- </fo:table>
- </fo:block>
- </fo:block-container>
- </fo:flow>
- <xsl:template name="generate-index-column">
- <xsl:param name="terms"/>
- <xsl:for-each select="$terms">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt" text-indent="-12pt" start-indent="12pt">
- <fo:inline font-weight="bold">
- <xsl:value-of select="@term"/>
- </fo:inline>
- <fo:inline>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:inline>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
复制代码
制表符和对齐
使用制表符可以确保页码对齐:
- <xsl:template name="generate-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt" text-align-last="justify">
- <fo:inline font-weight="bold">
- <xsl:value-of select="@term"/>
- </fo:inline>
- <fo:leader leader-pattern="dots"/>
- <fo:inline>
- <xsl:for-each select="key('index-terms', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:inline>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
复制代码
自动化索引生成
使用XSLT自动标记索引条目
对于大型文档,手动标记每个索引条目可能很耗时。我们可以编写XSLT自动识别并标记潜在索引条目:
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-
- <!-- 定义应该被索引的术语列表 -->
- <xsl:variable name="index-terms" select="document('index-terms.xml')/terms/term"/>
-
- <!-- 处理文本节点,查找索引术语 -->
- <xsl:template match="text()">
- <xsl:param name="text" select="."/>
- <xsl:choose>
- <!-- 检查是否有匹配的索引术语 -->
- <xsl:when test="$index-terms[contains($text, .)]">
- <xsl:call-template name="process-text">
- <xsl:with-param name="text" select="$text"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="$text"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <!-- 处理文本,添加索引标记 -->
- <xsl:template name="process-text">
- <xsl:param name="text"/>
-
- <!-- 找到最早出现的索引术语 -->
- <xsl:variable name="earliest-term">
- <xsl:for-each select="$index-terms[contains($text, .)]">
- <term pos="{string-length(substring-before($text, .))}" value="{.}"/>
- </xsl:for-each>
- </xsl:variable>
-
- <xsl:variable name="first-term" select="$earliest-term/term[@pos = min($earliest-term/term/@pos)][1]"/>
-
- <!-- 输出术语前的文本 -->
- <xsl:value-of select="substring($text, 1, $first-term/@pos)"/>
-
- <!-- 添加索引标记 -->
- <index-term term="{$first-term/@value}">
- <xsl:value-of select="$first-term/@value"/>
- </index-term>
-
- <!-- 处理剩余文本 -->
- <xsl:call-template name="process-text">
- <xsl:with-param name="text" select="substring-after($text, $first-term/@value)"/>
- </xsl:call-template>
- </xsl:template>
-
- <!-- 复制其他元素 -->
- <xsl:template match="@*|node()">
- <xsl:copy>
- <xsl:apply-templates select="@*|node()"/>
- </xsl:copy>
- </xsl:template>
- </xsl:stylesheet>
复制代码
使用正则表达式识别索引术语
更高级的方法是使用正则表达式识别索引术语:
- <xsl:stylesheet version="2.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
- xmlns:my="http://example.com/my"
- exclude-result-prefixes="xs my">
-
- <!-- 定义索引术语的正则表达式模式 -->
- <xsl:variable name="index-pattern" select="'\b(XSL FO|Formatting Objects|XML|XPath)\b'"/>
-
- <!-- 处理文本节点 -->
- <xsl:template match="text()">
- <xsl:analyze-string select="." regex="{$index-pattern}">
- <xsl:matching-substring>
- <index-term term="{.}">
- <xsl:value-of select="."/>
- </index-term>
- </xsl:matching-substring>
- <xsl:non-matching-substring>
- <xsl:value-of select="."/>
- </xsl:non-matching-substring>
- </xsl:analyze-string>
- </xsl:template>
-
- <!-- 复制其他元素 -->
- <xsl:template match="@*|node()">
- <xsl:copy>
- <xsl:apply-templates select="@*|node()"/>
- </xsl:copy>
- </xsl:template>
- </xsl:stylesheet>
复制代码
性能优化
处理大型文档索引
对于大型文档,索引生成可能会变得很慢。以下是一些优化技巧:
1. 使用键(key)提高效率:
- <xsl:key name="index-terms" match="index-term" use="@term"/>
复制代码
1. 分阶段处理:
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:fo="http://www.w3.org/1999/XSL/Format"
- xmlns:idx="http://example.com/index"
- exclude-result-prefixes="idx">
-
- <!-- 第一阶段:收集索引条目 -->
- <xsl:variable name="index-entries">
- <idx:entries>
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <idx:entry term="{@term}">
- <xsl:for-each select="key('index-terms', @term)">
- <idx:location>
- <xsl:value-of select="ancestor::chapter/title"/>
- </idx:location>
- </xsl:for-each>
- </idx:entry>
- </xsl:for-each>
- </idx:entries>
- </xsl:variable>
-
- <!-- 第二阶段:生成文档和索引 -->
- <xsl:template match="/">
- <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
- <!-- 文档内容 -->
- <fo:layout-master-set>
- <fo:simple-page-master master-name="main" page-height="29.7cm" page-width="21cm">
- <fo:region-body margin="2cm"/>
- </fo:simple-page-master>
- </fo:layout-master-set>
-
- <fo:page-sequence master-reference="main">
- <fo:flow flow-name="xsl-region-body">
- <!-- 处理文档内容 -->
- <xsl:apply-templates select="book"/>
-
- <!-- 生成索引 -->
- <fo:block break-before="page" font-size="18pt" font-weight="bold" margin-bottom="12pt">
- Index
- </fo:block>
- <xsl:apply-templates select="$index-entries"/>
- </fo:flow>
- </fo:page-sequence>
- </fo:root>
- </xsl:template>
-
- <!-- 处理索引条目 -->
- <xsl:template match="idx:entries">
- <fo:block font-family="Arial, sans-serif" font-size="10pt">
- <xsl:apply-templates select="idx:entry"/>
- </fo:block>
- </xsl:template>
-
- <xsl:template match="idx:entry">
- <fo:block margin-bottom="4pt" text-indent="-12pt" start-indent="12pt">
- <fo:inline font-weight="bold">
- <xsl:value-of select="@term"/>
- </fo:inline>
- <fo:inline>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="idx:location">
- <xsl:value-of select="."/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:inline>
- </fo:block>
- </xsl:template>
-
- <!-- 处理其他元素 -->
- <xsl:template match="chapter">
- <fo:block break-before="page" font-size="16pt" font-weight="bold" margin-bottom="10pt">
- <xsl:value-of select="title"/>
- </fo:block>
- <xsl:apply-templates select="para"/>
- </xsl:template>
-
- <xsl:template match="para">
- <fo:block margin-bottom="8pt">
- <xsl:apply-templates/>
- </fo:block>
- </xsl:template>
-
- <xsl:template match="index-term">
- <!-- 索引术语在正文中不显示 -->
- <xsl:apply-templates/>
- </xsl:template>
- </xsl:stylesheet>
复制代码
1. 使用XSLT 2.0或更高版本:XSLT 2.0及更高版本提供了更强大的功能和更好的性能。
内存优化
对于非常大的文档,内存可能成为问题。以下是一些减少内存使用的技巧:
1. 使用SAX解析器:SAX解析器比DOM解析器更节省内存。
2. 分块处理:将文档分成较小的块处理:
使用SAX解析器:SAX解析器比DOM解析器更节省内存。
分块处理:将文档分成较小的块处理:
- <xsl:stylesheet version="2.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:fo="http://www.w3.org/1999/XSL/Format"
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
- exclude-result-prefixes="xs">
-
- <!-- 使用collection()函数处理多个文件 -->
- <xsl:template name="process-large-document">
- <xsl:for-each select="collection('docs/?select=*.xml')">
- <xsl:apply-templates select="."/>
- </xsl:for-each>
- </xsl:template>
- </xsl:stylesheet>
复制代码
1. 使用流处理:XSLT 3.0支持流处理,可以处理非常大的文件而不会耗尽内存。
实际案例研究
技术手册索引
技术手册通常需要详细的索引,包括术语、命令、函数等:
- <!-- 示例:技术手册索引条目 -->
- <para>The <index-term term="configure" subterm="command"/>configure command
- is used to set system parameters. See also
- <index-term term="setup" subterm="process" see="configure"/>setup process.</para>
- <!-- 技术手册索引XSLT -->
- <xsl:template name="generate-technical-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="6pt">
- <fo:inline font-weight="bold" font-family="Courier New, monospace">
- <xsl:value-of select="@term"/>
- </fo:inline>
- <xsl:choose>
- <xsl:when test="@see">
- <xsl:text> see </xsl:text>
- <fo:inline font-family="Courier New, monospace">
- <xsl:value-of select="@see"/>
- </fo:inline>
- </xsl:when>
- <xsl:otherwise>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)[not(@subterm)]">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </xsl:otherwise>
- </xsl:choose>
- </fo:block>
-
- <!-- 子术语 -->
- <xsl:for-each select="//index-term[@term = current()/@term and @subterm][generate-id() = generate-id(key('subterms', concat(@term, '::', @subterm))[1])]">
- <xsl:sort select="@subterm"/>
- <fo:block margin-left="12pt" margin-bottom="6pt">
- <fo:inline font-family="Courier New, monospace">
- <xsl:value-of select="@subterm"/>
- </fo:inline>
- <xsl:choose>
- <xsl:when test="@see">
- <xsl:text> see </xsl:text>
- <fo:inline font-family="Courier New, monospace">
- <xsl:value-of select="@see"/>
- </fo:inline>
- </xsl:when>
- <xsl:otherwise>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('subterms', concat(@term, '::', @subterm))">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </xsl:otherwise>
- </xsl:choose>
- </fo:block>
- </xsl:for-each>
- </xsl:for-each>
- </xsl:template>
复制代码
法律文档索引
法律文档索引需要精确引用和特殊的格式:
- <!-- 示例:法律文档索引条目 -->
- <para>According to <index-term term="Copyright Act" section="107(3)"/>Copyright Act § 107(3),
- fair use includes the purpose and character of the use. See also
- <index-term term="Fair Use" subterm="four factors" see="Copyright Act, 107(3)"/>.</para>
- <!-- 法律文档索引XSLT -->
- <xsl:template name="generate-legal-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="6pt">
- <fo:inline font-weight="bold" font-style="italic">
- <xsl:value-of select="@term"/>
- </fo:inline>
- <xsl:choose>
- <xsl:when test="@see">
- <xsl:text> see </xsl:text>
- <fo:inline font-style="italic">
- <xsl:value-of select="@see"/>
- </fo:inline>
- </xsl:when>
- <xsl:otherwise>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)[not(@subterm)]">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="@section">
- <xsl:text> §</xsl:text>
- <xsl:value-of select="@section"/>
- </xsl:if>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </xsl:otherwise>
- </xsl:choose>
- </fo:block>
-
- <!-- 子术语 -->
- <xsl:for-each select="//index-term[@term = current()/@term and @subterm][generate-id() = generate-id(key('subterms', concat(@term, '::', @subterm))[1])]">
- <xsl:sort select="@subterm"/>
- <fo:block margin-left="12pt" margin-bottom="6pt">
- <fo:inline font-style="italic">
- <xsl:value-of select="@subterm"/>
- </fo:inline>
- <xsl:choose>
- <xsl:when test="@see">
- <xsl:text> see </xsl:text>
- <fo:inline font-style="italic">
- <xsl:value-of select="@see"/>
- </fo:inline>
- </xsl:when>
- <xsl:otherwise>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('subterms', concat(@term, '::', @subterm))">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="@section">
- <xsl:text> §</xsl:text>
- <xsl:value-of select="@section"/>
- </xsl:if>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </xsl:otherwise>
- </xsl:choose>
- </fo:block>
- </xsl:for-each>
- </xsl:for-each>
- </xsl:template>
复制代码
学术论文索引
学术论文索引可能需要包括作者、主题和方法等多种索引:
- <!-- 示例:学术论文索引条目 -->
- <para>As <index-term type="author" name="Smith"/>Smith (2020) argues,
- <index-term type="subject" term="qualitative research"/>qualitative research
- provides deeper insights into <index-term type="method" term="interviews"/>interviews.</para>
- <!-- 学术论文索引XSLT -->
- <xsl:key name="author-index" match="index-term[@type='author']" use="@name"/>
- <xsl:key name="subject-index" match="index-term[@type='subject']" use="@term"/>
- <xsl:key name="method-index" match="index-term[@type='method']" use="@term"/>
- <xsl:template name="generate-academic-index">
- <!-- 作者索引 -->
- <fo:block font-size="14pt" font-weight="bold" margin-top="12pt" margin-bottom="8pt">
- Author Index
- </fo:block>
- <xsl:for-each select="//index-term[@type='author'][generate-id() = generate-id(key('author-index', @name)[1])]">
- <xsl:sort select="@name"/>
- <fo:block margin-bottom="4pt">
- <xsl:value-of select="@name"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('author-index', @name)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
- </xsl:for-each>
-
- <!-- 主题索引 -->
- <fo:block font-size="14pt" font-weight="bold" margin-top="12pt" margin-bottom="8pt">
- Subject Index
- </fo:block>
- <xsl:for-each select="//index-term[@type='subject'][generate-id() = generate-id(key('subject-index', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('subject-index', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
- </xsl:for-each>
-
- <!-- 方法索引 -->
- <fo:block font-size="14pt" font-weight="bold" margin-top="12pt" margin-bottom="8pt">
- Method Index
- </fo:block>
- <xsl:for-each select="//index-term[@type='method'][generate-id() = generate-id(key('method-index', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('method-index', @term)">
- <xsl:value-of select="ancestor::chapter/title"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
复制代码
常见问题和解决方案
问题1:索引条目排序不正确
问题:索引条目没有按字母顺序正确排序,特别是涉及特殊字符或不同语言时。
解决方案:使用适当的排序规则和语言设置:
- <xsl:sort select="@term" lang="en" data-type="text" collation="http://www.w3.org/2013/collation/UCA?lang=en"/>
复制代码
问题2:页码引用不准确
问题:索引中的页码与实际内容位置不匹配。
解决方案:确保在最终生成阶段使用正确的页码引用,可能需要两遍处理:
- <!-- 第一遍:收集所有索引条目 -->
- <xsl:variable name="raw-index">
- <xsl:for-each select="//index-term">
- <entry term="{@term}" page="{generate-id()}"/>
- </xsl:for-each>
- </xsl:variable>
- <!-- 第二遍:将生成的页码与索引条目匹配 -->
- <xsl:template name="generate-index">
- <xsl:for-each select="$raw-index/entry[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
- <xsl:for-each select="key('index-terms', @term)">
- <xsl:variable name="page-id" select="@page"/>
- <xsl:value-of select="id($page-id)/@page-number"/>
- <xsl:if test="position() != last()">, </xsl:if>
- </xsl:for-each>
- </fo:block>
- </xsl:for-each>
- </xsl:template>
复制代码
问题3:索引条目重复
问题:同一术语在索引中出现多次,而不是合并为一个条目。
解决方案:确保使用键(key)正确分组相同的术语:
- <xsl:key name="index-terms" match="index-term" use="normalize-space(translate(@term, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))"/>
- <xsl:template name="generate-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', normalize-space(translate(@term, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')))[1])]">
- <xsl:sort select="normalize-space(translate(@term, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))"/>
- <!-- 其余索引生成代码 -->
- </xsl:for-each>
- </xsl:template>
复制代码
问题4:索引格式不一致
问题:索引中的术语格式不一致,有些使用大写,有些使用小写。
解决方案:在生成索引时统一术语格式:
- <xsl:template name="generate-index">
- <xsl:for-each select="//index-term[generate-id() = generate-id(key('index-terms', @term)[1])]">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt">
- <!-- 首字母大写,其余小写 -->
- <xsl:value-of select="concat(upper-case(substring(@term, 1, 1)), lower-case(substring(@term, 2)))"/>
- <xsl:text>, </xsl:text>
- <!-- 其余代码 -->
- </fo:block>
- </xsl:for-each>
- </xsl:template>
复制代码
问题5:索引生成速度慢
问题:对于大型文档,索引生成过程非常缓慢。
解决方案:优化XSLT代码并使用更高效的处理器:
- <!-- 使用XSLT 2.0或更高版本,并优化键的使用 -->
- <xsl:stylesheet version="2.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:fo="http://www.w3.org/1999/XSL/Format">
-
- <!-- 使用更高效的键定义 -->
- <xsl:key name="index-terms" match="index-term" use="@term"/>
-
- <!-- 使用xsl:for-each-group而不是generate-id()检查 -->
- <xsl:template name="generate-index">
- <xsl:for-each-group select="//index-term" group-by="@term">
- <xsl:sort select="@term"/>
- <fo:block margin-bottom="4pt">
- <xsl:value-of select="@term"/>
- <xsl:text>, </xsl:text>
- <xsl:value-of select="current-group()/ancestor::chapter/title" separator=", "/>
- </fo:block>
- </xsl:for-each-group>
- </xsl:template>
- </xsl:stylesheet>
复制代码
结论和最佳实践
XSL FO提供了创建专业文档索引的强大功能。通过掌握本文介绍的技术和技巧,您可以创建出结构清晰、格式统一、易于使用的专业索引,显著提升文档的信息检索效率和用户体验。
最佳实践总结
1. 规划索引结构:在开始之前,仔细规划索引的结构和层次,确保它符合用户的需求和期望。
2. 使用一致的术语:确保索引中使用一致的术语和格式,避免混淆。
3. 考虑用户需求:从用户的角度思考,他们可能会如何搜索信息,并相应地组织索引。
4. 自动化索引生成:尽可能使用自动化工具生成索引,减少手动工作量和错误。
5. 测试和验证:在最终发布前,彻底测试索引,确保所有条目准确无误且格式一致。
6. 优化性能:对于大型文档,使用适当的优化技术确保索引生成过程高效。
7. 保持更新:随着文档的更新,及时更新索引,确保其始终准确反映文档内容。
规划索引结构:在开始之前,仔细规划索引的结构和层次,确保它符合用户的需求和期望。
使用一致的术语:确保索引中使用一致的术语和格式,避免混淆。
考虑用户需求:从用户的角度思考,他们可能会如何搜索信息,并相应地组织索引。
自动化索引生成:尽可能使用自动化工具生成索引,减少手动工作量和错误。
测试和验证:在最终发布前,彻底测试索引,确保所有条目准确无误且格式一致。
优化性能:对于大型文档,使用适当的优化技术确保索引生成过程高效。
保持更新:随着文档的更新,及时更新索引,确保其始终准确反映文档内容。
通过遵循这些最佳实践,您可以充分利用XSL FO的强大功能,创建出专业、高效、用户友好的文档索引,为读者提供卓越的信息检索体验。
版权声明
1、转载或引用本网站内容(XSL FO文档索引制作全指南从基础入门到高级应用技巧详解如何利用这一强大工具创建专业文档索引提升信息检索效率)须注明原网址及作者(威震华夏关云长),并标明本网站网址(https://www.pixtech.cc/)。
2、对于不当转载或引用本网站内容而引起的民事纷争、行政处理或其他损失,本网站不承担责任。
3、对不遵守本声明或其他违法、恶意使用本网站内容者,本网站保留追究其法律责任的权利。
本文地址: https://www.pixtech.cc/thread-34928-1-1.html
|
|