如何在XSLT转换中仅当目标页码与当前页不同时插入XSL-FO值?
可以在XSLT转换阶段实现该逻辑
因为XSLT转换时无法提前获取PDF的最终页码值(这些是格式化引擎Antenna House在渲染阶段计算的),所以我们需要生成带条件判断的XSL-FO代码,让格式化引擎在渲染时自动判断是否显示页码标识。
修改后的代码示例
<fo:table-cell> <!-- 仅当引用页码与当前页码不同时,才显示带#的标识块 --> <fo:block display-condition="fo:page-number-citation-last(ref-id='{self::condition/@id}') ne fo:page-number()"> <xsl:text>#</xsl:text> <xsl:call-template name="setPage"/> <xsl:text>#</xsl:text> </fo:block> </fo:table-cell> <xsl:template name="setPage"> <xsl:choose> <xsl:when test="ancestor::xpto"> <xsl:value-of select="@nrb"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="ancestor-or-self::qrew/@pstr"/> </xsl:otherwise> </xsl:choose> <xsl:choose> <xsl:when test="ancestor::xpto">N- <fo:page-number-citation-last> <xsl:attribute name="ref-id"><xsl:value-of select="self::condition/@id"/></xsl:attribute> </fo:page-number-citation-last> </xsl:when> <xsl:otherwise> <xsl:value-of select="ancestor::block/@blknbr"/>SSZ- <fo:page-number-citation-last> <xsl:attribute name="ref-id"><xsl:value-of select="self::condition/@id"/></xsl:attribute> </fo:page-number-citation-last> </xsl:otherwise> </xsl:choose> </xsl:template>
关键说明
display-condition是XSL-FO 1.1规范属性,Antenna House完全支持。它的值是一个FO表达式,用于控制元素是否显示。fo:page-number()获取当前页面的页码,fo:page-number-citation-last(ref-id='xxx')获取目标元素的最后出现页码,两者通过ne(不等于)运算符比较。- 将带#的内容包裹在条件块中,当引用页码和当前页码相同时,整个块会被Antenna House自动忽略,无需后续处理。
如果需要处理更复杂的场景(比如页码前缀/后缀的条件显示),可以把判断逻辑细化到setPage模板内部,仅在条件满足时输出对应内容。
内容的提问来源于stack exchange,提问作者Noah
相关产品推荐
相关产品推荐

