如何在xml节点之后添加换行符,以便FOP识别空白? [英] How to add newline after an xml node so that FOP will recognize whitespace?

查看:203
本文介绍了如何在xml节点之后添加换行符,以便FOP识别空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将Apache FOP 1.1与XSLT结合使用,以生成用于打印的PDF.我们的软件正确创建了XML,并将其直接传递给FOP转换器.我们不会在文件中保存任何内容,因此使用转换器属性来缩进XML不会在这里有所帮助.

We're using Apache FOP 1.1 with XSLT to generate a PDF for printing. Our software is creating the XML correctly, and passing this directly to the FOP transformer. We are not saving anything to file so the use of transformer properties to indent the XML is not going to help here.

基本上,我们的XML生成器会创建一个如下所示的XML文档:

Basically our XML generator creates an XML document that looks like this:

<document><list><item>1</item><item>2</item><item>3</item></list></document>

,而我们的XSLT拥有一个位,该位占用list节点的每个item子级,并假定将它们打印出来,并在两者之间留有空格.像这样:

and our XSLT has a bit that takes each item child of the list node and is suppose to print them out with a space in between. Something like this:

<!DOCTYPE xsl:stylesheet [
     <!ENTITY nbsp '<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#160;</xsl:text>'>
]>
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" /> 
 <xsl:template match="list">
     <fo:inline>Items:&nbsp;</fo:inline>
     <xsl:for-each select="item">
          <xsl:value-of select="./text()"/>&nbsp;
     </xsl:for-each>
</xsl:template>

在XSLT文档的最顶部,我已经告诉它实体&nbsp;​​是什么,所以这不应该成为问题.问题在于,生成文件时,项目而不是显示为Items: 1 2 3而是显示为Items: 123.我能找到的似乎唯一可以解决该问题的方法是,当我生成XML并将其转换为字符串后,文档现在将如下所示:

At the very top of the XSLT document I've already told it what the entity &nbsp; is so that shouldn't be the problem. The problem is that when the file is generated the items instead of showing up as Items: 1 2 3 are showing up as Items: 123. The only thing I can find that seems to fix the problem is when I've generated the XML then transform it to a String where the document would now look like this:

<document>
     <list>
          <item>1</item>
          <item>2</item>
          <item>3</item>
     </list>
</document>

XSLT是否正确输出列表.因此,我想做的是通过DOM在列表中的每个项目之后添加一个换行节点,而不必先对其进行转换并将其保存到文件中.这可能吗?

Does the XSLT output the list correctly. So what I would like to do is add a newline node after each item in the list via the DOM, without having to first transform it and save it to a file. Is this possible?

更新! 我想向所有帮助的人致歉.给出了正确的答案,但是由于我处理的是模板的错误部分,所以我看不到纠正的效果.是的,正如您所指出的,由于其性质和来源,我没有发布整个模板.几年前,我启动了模板并使其正常工作,后来另一位开发人员对模板进行了更改,并停止使用最初用于工作的子模板.所以当我回到它的时候,我当然在我认为它正在工作的地方工作,并且想知道为什么我没有看到这些变化.

UPDATE! I would like to apologize to all those who helped. The correct answer was given, but since I was working on the wrong portion of the template, I wasn't seeing the correction working. Yes as you noted I didn't post the entire template, that's due to its nature and source. Some years ago I started the template and had it working, and later another developer made changes to the template and stopped using the sub-template where I was originally doing the work. So when I came back to it I was of course working where I thought it was working, and was wondering why I wasn't seeing the changes.

感谢您的支持.这个问题解决了.

Thanks for the support in this. This issue is solved.

推荐答案

您的实体声明有点混乱.其中有一个名称空间声明,&#160不以分号结尾.

Your entity declaration is a bit of a mess. There is a namespace declaration inside it, &#160 does not end with a semicolon.

在不将文本甚至单个字符包含在xsl:text元素中的情况下,请勿输出它们.这样可以确保它们已正确序列化.换句话说,改变

Do not output text or even single characters without enclosing them in xsl:text elements. This ensures that they are serialized correctly. In other words, change

&nbsp;

<xsl:text>&nbsp;</xsl:text>

我还简化了您的实体声明.在实体声明中包含XSLT元素和名称空间声明是没有意义的.

I also simplified your entity declaration. It does not make sense to include XSLT elements and a namespace declaration in an entity declaration.

无论如何,您都在xsl:stylesheet上声明了XSLT命名空间,而无需重新声明它. xsl:text元素应该放在实际代码中,而不是放在实体声明中.

You declare the XSLT namespace on xsl:stylesheet anyway and there is no need to redeclare it. xsl:text elements should be placed in actual code, not inside an entity declaration.

输入

<document><list><item>1</item><item>2</item><item>3</item></list></document>

样式表

<!DOCTYPE xsl:stylesheet [
     <!ENTITY nbsp '&#160;'>
]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" /> 

<xsl:template match="list">
     <fo:inline>
     <xsl:text>Items:&nbsp;</xsl:text>
     <xsl:for-each select="item">
          <xsl:value-of select="."/>
          <xsl:text>&nbsp;</xsl:text>
     </xsl:for-each>
     </fo:inline>
</xsl:template>

</xsl:stylesheet>

输出

<?xml version="1.0" encoding="utf-8"?>
<fo:inline xmlns:fo="http://www.w3.org/1999/XSL/Format">Items: 1 2 3 </fo:inline>

这篇关于如何在xml节点之后添加换行符,以便FOP识别空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆