如何在xsl-fo中正确显示制表? [英] How to show correctly tabulation in xsl-fo?
问题描述
我有一个XML文档,我正在创建一个XSL-FO文件,以使用apache-fop将其转换为pdf.
I have an XML document and I am creating an XSL-FO file to convert it to pdf with apache-fop.
在xml中,有< code>部分显示...代码.
In the xml, there are sections <code> to show... code.
在xsl-fo中,我添加了white-space ="pre"语句以保留代码格式,但是列表显示为单个空格:
In the xsl-fo, I added the white-space="pre" sentence to preserve the code format, but tabulations are shown like single space:
XML部分:
<code><![CDATA[
function callback( widget )
{
var ui = widget; // It should be a tab
}
]]></code>
XSL-FO部分:
<xsl:template match="code">
<fo:block font-size="10pt" font-family="monospace" white-space="pre" color="#008000" background-color="#f8f8f8">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
生成的PDF:
function callback( widget )
{
var ui = widget; // It should be a tab
}
所以我的问题是:如何保留或设置表格的大小?
我正在使用apache-fop 1.1
Edited: I am using apache-fop 1.1
完整示例:
proyecto.xml(不要忘记在"var ui ..."之前用制表符代替4个空格)
proyecto.xml (do not forget to replace the 4 spaces by a tab before "var ui...")
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="proyecto.xsl"?>
<document>
<code><![CDATA[
function callback( widget )
{
var ui = widget; // It should be a tab
}
]]></code>
</document>
proyecto.xsl
proyecto.xsl
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match ="document">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="OnePage" margin="1in"
page-height="29.7cm"
page-width="21cm"
margin-top="2.5cm"
margin-bottom="2.5cm"
margin-left="3.5cm"
margin-right="2.5cm">
<fo:region-body margin="0cm"/>
</fo:simple-page-master>
<fo:page-sequence-master master-name="Page">
<fo:single-page-master-reference master-reference="OnePage"/>
</fo:page-sequence-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="code">
<fo:block font-size="10pt" font-family="monospace" white-space="pre">
<xsl:apply-templates/>
<!--<xsl:value-of select="replace( . , 'a', 'b')"/>-->
</fo:block>
</xsl:template>
</xsl:stylesheet>
PDF结果(屏幕):
PDF result (screen):
推荐答案
按注释中的@mzjn的建议,将选项卡替换为四个空格.
Replace the tabs by four spaces, as suggested by @mzjn already in the comments.
XML输入
<code><![CDATA[
function callback( widget )
{
	var ui = widget; // It should be a tab
}
]]></code>
XSLT样式表
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4-portrait"
page-height="29.7cm" page-width="21.0cm" margin="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4-portrait">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="10pt" font-family="monospace" linefeed-treatment="preserve" white-space-collapse="false" white-space-treatment="preserve" wrap-option="no-wrap" color="#008000" background-color="#f8f8f8">
<xsl:value-of select="replace(code,'	',' ')"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
现在,用空格替换制表符后,输出将由FOP正确呈现.
Now, after replacing the tab characters with whitespaces, the output is rendered correctly by FOP.
输出选项卡没有明显的缺陷,但是请记住,就whitespace-treatment
属性而言,Apache FOP仅部分兼容:
There is no obvious flaw with outputting tabs, but bear in mind that Apache FOP is only partially compliant in regard to the whitespace-treatment
property: http://xmlgraphics.apache.org/fop/compliance.html , even if it says that only fo:inline
elements are affected of this.
这篇关于如何在xsl-fo中正确显示制表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!