【发布时间】:2012-12-11 08:02:17
【问题描述】:
我有这个 XSLT 2.0 模板:
<xsl:template match="footnote">
<xsl:variable name = "string" select="./text()"/>
<xsl:variable name = "bool">
<xsl:choose>
<xsl:when test="$string = preceding::footnote/text()">
<xsl:text>false</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>true</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="$bool = 'true'">
<xsl:variable name="footnoteCount">
<xsl:call-template name="getItemNumber">
<xsl:with-param name="node" select="."/>
</xsl:call-template>
</xsl:variable>
<!-- DO XSL-FO TRANSFORMATION STUFF-->
</xsl:if>
<xsl:if test="$bool = 'false'">
<xsl:variable name = "footnoteCount">
<xsl:if test="$string = preceding::footnote/text()">
<xsl:value-of select="preceding::footnote/$footnoteCount"/>
</xsl:if>
</xsl:variable>
<!--DO XSL-FO TRANSFORMATION STUFF-->
</xsl:if>
</xsl:template>
编辑了示例 XML。我想改变这个:
<footnote>Foo bar</footnote>
<footnote>Bar foo</footnote>
<footnote>Foo bar</footnote>
<footnote>Foo bar</footnote>
<footnote>Bar</footnote>
<footnote>Foo</footnote>
进入这个:
<footnote>Foo bar</footnote>
<footnote>Bar foo</footnote>
<footnote>Bar</footnote>
<footnote>Foo</footnote
然后使用 XSL-FO 对其进行样式化。这种样式的目的是主体中的文本将有一个编号的引用,由$footnotecount 表示,然后在页面底部呈现脚注。我需要转换文档,以便重复的脚注只呈现一次,并且每个重复的数字参考 ($footnoteCount) 都是相同的。
所以我想用这个模板做的是:
- 确定是否已经存在具有当前节点文本的脚注元素。
- 如果它不存在(即,'$bool' 为 'true'),找到前一个脚注的编号,增加它(这在 'getItemNumber' 模板中完成)并创建一个“新”脚注。如果确实存在($bool 为 'false'),则获取文本匹配的节点的 $footnoteCount 变量,并将其用于当前节点。
这是我遇到问题的脚注已经存在的场景。我不知道如何从先前的特定节点获取 $footnoteCount 变量,具体取决于节点是否满足特定条件(其文本是否与当前节点中的 $string 变量相同)。由于 $footnoteCount 变量只能有条件地存在(即使在实践中它总是存在,因为 $bool 必须为真或假),这一事实变得更加困难。
有人对在这里做什么有建议吗?
【问题讨论】:
-
能否请您也发布
getItemNumber模板。一旦我知道你打算做什么,我可以优化代码! -
我不认为 getItemNumber 模板与问题相关 - 它只是生成一个数字,如果当前节点文本的脚注不存在,则使用该数字。当包含当前节点文本的脚注确实存在时,我更关心会发生什么。另外,getItemNumber 模板调用了其他三个模板,如果我将它们全部发布,那么这个问题会变得非常复杂。 :)
-
您使用的是 XSLT1.0 还是 XSLT2.0?
-
另外,澄清一下......如果你有
Footnote 1, Footnote 2, Footnote 4,输出会是Footnote 1, Footnote 2, Footnote 3吗? -
不,
Footnote 1, Footnote 2只是占位符数据——实际上它只是任何字符串。这个想法是摆脱“重复”脚注,而不是按照文本中出现的数字对它们进行排序。