xslt - How to show correctly tabulation in xsl-fo? -
i have xml document , creating xsl-fo file convert pdf apache-fop.
in xml, there sections <code> show... code.
in xsl-fo, added white-space="pre" sentence preserve code format, tabulations shown single space:
xml section:
<code><![cdata[ function callback( widget ) { var ui = widget; // should tab } ]]></code>
xsl-fo section:
<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>
resulting pdf:
function callback( widget ) { var ui = widget; // should tab }
so question is: how preserve or set size of tabulation?
edited: using apache-fop 1.1
a full example:
proyecto.xml (do not forget replace 4 spaces 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; // should tab } ]]></code> </document>
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 result (screen):
replace tabs 4 spaces, suggested @mzjn in comments.
xml input
<code><![cdata[ function callback( widget ) { 	var ui = widget; // should tab } ]]></code>
xslt stylesheet
<?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>
now, after replacing tab characters whitespaces, output rendered correctly fop.
there no obvious flaw outputting tabs, bear in mind apache fop partially compliant in regard whitespace-treatment
property: http://xmlgraphics.apache.org/fop/compliance.html , if says fo:inline
elements affected of this.
Comments
Post a Comment