[英]Adding line break in a PDF with XSL-FO?
Trying to create a PDF file based on an XML and a file using XMLSpy.尝试基于 XML 和使用 XMLSpy 的文件创建 PDF 文件。
I'm trying to split a field into two lines based on the field content.我正在尝试根据字段内容将字段拆分为两行。
For example, if my varialbe = "John Doe AKA Johnny D", I want to view it like this:例如,如果我的 varialbe = "John Doe AKA Johnny D",我想这样查看:
John Doe约翰·多伊
Johnny D约翰尼 D
My problem is that I can't make it work even with all the samples on the net.我的问题是,即使使用网络上的所有样本,我也无法使其工作。
Here's my code:这是我的代码:
<xsl:value-of disable-output-escaping="yes" select="concat(substring-before(//MyField,'AKA'),$newline,substring-after(//MyField,'AKA'))" />
</xsl:when>
So basically, eveytime I find the "AKA" string, I want to break the field into two lines.所以基本上,每次我找到“AKA”字符串时,我想将该字段分成两行。 So my code, finds the string, create the new variable but still shows in one line.所以我的代码找到了字符串,创建了新变量,但仍然显示在一行中。 I've tried creating a variable with a blank line, using all kinds of techiniques but still shows in one line.我尝试使用各种技术创建一个带有空行的变量,但仍显示在一行中。
Any thoughts?有什么想法吗?
See my answer here about using a hex entity reference and linefeed-treatment
.请参阅我关于使用十六进制实体引用和linefeed-treatment
的答案。
Edit编辑
I took your code from the comments and put it in template in a sample XSLT stylesheet.我从评论中获取了您的代码,并将其放入示例 XSLT 样式表中的模板中。 The only thing I changed was:我唯一改变的是:
newline
variable to 

我将您的newline
变量更改为

. .linefeed-treatment="preserve"
to your fo:block
.我在您的fo:block
中添加了linefeed-treatment="preserve"
。Using a dummy XML file and the XSLT stylesheet, I produced an XSL-FO document that when rendered with FOP, produces "John Doe" and "Johnny D" on separate lines.使用虚拟 XML 文件和 XSLT 样式表,我生成了一个 XSL-FO 文档,当使用 FOP 呈现时,该文档在不同的行上生成“John Doe”和“Johnny D”。
Here is the XML file:这是 XML 文件:
<doc>
<MyField>John Doe AKA Johnny D</MyField>
</doc>
Here is the XSLT stylesheet:这是 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:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="doc">
<xsl:variable name="newline" select="'
'"/>
<xsl:variable name="MyVar">
<xsl:choose>
<xsl:when test="contains(//MyField,'AKA')">
<xsl:value-of select="concat(substring-before(//MyField,'AKA'),$newline,substring-after(//MyField,'AKA'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//MyField"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<fo:block linefeed-treatment="preserve">
<xsl:value-of select="$MyVar"/>
</fo:block>
</xsl:template>
</xsl:stylesheet>
Here is the resulting XSL-FO:这是生成的 XSL-FO:
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master page-height="11in" page-width="8.5in" master-name="my-page">
<fo:region-body margin-top="1.5in" margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block linefeed-treatment="preserve">John Doe
Johnny D</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</fo:flow>
</fo:page-sequence>
</fo:root>
The PDF is a single 8.5" x 11" page with this on it: PDF 是一个 8.5" x 11" 的单页,上面有这个:
John Doe
Johnny D
The answer by @daniel-haley still produces a single pair of names when the source is:当来源是:@daniel-haley 的答案仍然会产生一对名称:
<doc>
<MyField>John Doe AKA Johnny D</MyField>
<MyField>Johnny D AKA John Doe</MyField>
<MyField>John Smith</MyField>
</doc>
(In XPath 1.0, converting a node-set to a string returns the string value of only the node in the node-set that is first in document order. See https://www.w3.org/TR/xpath/#function-string .) (在 XPath 1.0 中,将节点集转换为字符串仅返回节点集中文档顺序中第一个节点的字符串值。参见https://www.w3.org/TR/xpath/#function -字符串。)
The stylesheet below splits any text node containing " AKA
".下面的样式表拆分任何包含“ AKA
”的文本节点。 Since the enclosing fo:block
comes from the xsl:template
for MyField
, this version generates an empty fo:block
to cause the line break.由于封闭的fo:block
来自MyField
的xsl:template
,因此此版本会生成一个空的fo:block
以导致换行。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates />
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- Could change to match on 'MyField/text()[contains(., ' AKA ')]'
if necessary. -->
<xsl:template match="text()[contains(., ' AKA ')]">
<xsl:value-of select="substring-before(., ' AKA ')" />
<fo:block />
<xsl:value-of select="substring-after(., ' AKA ')" />
</xsl:template>
<xsl:template match="MyField">
<fo:block>
<xsl:apply-templates />
</fo:block>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.