我想为XSLT中的文本输出生成换行符。什么好主意吗?
当前回答
<xsl:text> </xsl:text>
参见示例
<xsl:variable name="module-info">
<xsl:value-of select="@name" /> = <xsl:value-of select="@rev" />
<xsl:text> </xsl:text>
</xsl:variable>
如果你把它写在文件中。
<redirect:write file="temp.prop" append="true">
<xsl:value-of select="$module-info" />
</redirect:write>
这个变量将生成一个新的行文件,如下:
commons-dbcp_commons-dbcp = 1.2.2
junit_junit = 4.4
org.easymock_easymock = 2.4
其他回答
我最喜欢的方法如下:
<xsl:stylesheet>
<xsl:output method='text'/>
<xsl:variable name='newline'><xsl:text>
</xsl:text></xsl:variable>
<!-- note that the layout there is deliberate -->
...
</xsl:stylesheet>
然后,无论何时你想输出换行符(也许在csv中),你都可以输出如下内容:
<xsl:value-of select="concat(elem1,elem2,elem3,$newline)" />
我在从xml输入输出sql时使用过这种技术。事实上,我倾向于为逗号、引号和换行符创建变量。
我发现了<xsl:text>中的文字换行符和使用
的文字换行符之间的区别。
虽然文字换行符在我的环境中工作得很好(使用Saxon和默认的Java XSLT处理器),但当在. net环境中运行的另一个组执行代码时,我的代码失败了。
更改为实体(
)使我的文件生成代码在Java和. net上一致地运行。
此外,文字换行符很容易被ide重新格式化,当文件由“不知情”的人维护时,可能会无意中丢失。
在xsl:output标记中包含属性Method="text",并在xsl中适当的位置在文本内容中包含换行符。如果您希望XSL源代码保持整洁,请使用实体 你想要一条新线。
<xsl:text xml:space="preserve">
</xsl:text>
恕我直言,除了@Florjon提供的信息,我们不需要更多的信息。也许会留下一些小细节来理解为什么它有时不适合我们。
首先,<xsl:text/>中的
 (hex)或
 (dec)总是有效的,但您可能看不到它。
There is no newline in a HTML markup. Using a simple <br/> will do fine. Otherwise you'll see a white space. Viewing the source from the browser will tell you what really happened. However, there are cases you expect this behaviour, especially if the consumer is not directly a browser. For instance, you want to create an HTML page and view its structure formatted nicely with empty lines and idents before serving it to the browser. Remember where you need to use disable-output-escaping and where you don't. Take the following example where I had to create an xml from another and declare its DTD from a stylesheet.
第一个版本转义字符(默认为xsl:text)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="/">
<xsl:text><!DOCTYPE Subscriptions SYSTEM "Subscriptions.dtd">


</xsl:text>
<xsl:copy>
<xsl:apply-templates select="*" mode="copy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()" mode="copy">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="copy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Subscriptions SYSTEM "Subscriptions.dtd">
<Subscriptions>
<User id="1"/>
</Subscriptions>
好的,它做了我们期望的,转义,这样我们使用的字符就能正确显示。根节点内的XML部分格式化由ident="yes"处理。但是仔细看看,我们会发现换行符
并没有转义和翻译,而是执行了双换行!我对此没有解释,知道就好了。有人知道吗?
第二个版本没有脱离角色,所以他们正在制作他们想要的东西。所作的更改如下:
<xsl:text disable-output-escaping="yes"><!DOCTYPE Subscriptions SYSTEM "Subscriptions.dtd">


</xsl:text>
结果如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Subscriptions SYSTEM "Subscriptions.dtd">
<Subscriptions>
<User id="1"/>
</Subscriptions>
这样就可以了。cr和lf都被正确呈现。
别忘了我们说的是nl,不是crlf (nl=lf)。我的第一次尝试是只使用cr:
,而输出xml是由DOM正确验证的。
我正在查看一个损坏的xml:
<?xml version="1.0" encoding="utf-8"?>
<Subscriptions>riptions SYSTEM "Subscriptions.dtd">
<User id="1"/>
</Subscriptions>
DOM解析器会忽略控制字符,但呈现的控件不会。我花了很长时间撞了撞头,才意识到我没有看到这个是多么愚蠢!
为了记录,我确实在两个CRLF的体内使用了一个变量,只是为了100%确定它在任何地方都可以工作。