xml 如何在我使用 XSLT 创建的文本中添加换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4799371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:30:45  来源:igfitidea点击:

How can I add line break into text that I am creating with XSLT?

xmlxslt

提问by bjorsig

I am trying to create text output from an xml file using xslt. It is actually an xslt that creates SQL code. Here is a part that outputs CREATE TABLE statements:

我正在尝试使用 xslt 从 xml 文件创建文本输出。它实际上是一个创建 SQL 代码的 xslt。这是输出 CREATE TABLE 语句的部分:

CREATE TABLE dbo.[<xsl:value-of select="@PhysicalName"/>] (
  <xsl:for-each select="EntityAttributes/EntityAttribute">
    <xsl:apply-templates select="Attributes/Attribute[@AttributeID = current()/@EntityAttributeID]"/> ...
  </xsl:for-each>)

I want to have a line break after the "(" in the first line but cannot manage to find out how to do so. Can anyone help?

我想在第一行的“(”之后换行,但无法找到如何做到这一点。有人可以帮忙吗?

采纳答案by peter.murray.rust

If you put

如果你把

<xsl:text>
</xsl:text>

in your XSLT it will give a line break. It is not clear where you wish to put this. I am guessing you want:

在您的 XSLT 中,它会换行。不清楚你想把它放在哪里。我猜你想要:

<xsl:text>CREATE TABLE dbo.[</xsl:text><xsl:value-of select="@PhysicalName"/><xsl:text>] (
</xsl:text>

In general you should always wrap text output in ... it looks slightly horrid in the XSL but it preserves the spacing. Note that you can break the lines in the XSLT without affecting the result - e.g.

一般而言,您应该始终将文本输出包装在 ... 它在 XSL 中看起来有点可怕,但它保留了间距。请注意,您可以在不影响结果的情况下在 XSLT 中换行 - 例如

<xsl:text>CREATE TABLE dbo.[</xsl:text>
<xsl:value-of select="@PhysicalName"/>
<xsl:text>] (&#xa;</xsl:text>

and yes, I agree about the explicit line break character. As you can see the XSLT is not very readable but it gets the right answer

是的,我同意明确的换行符。如您所见,XSLT 的可读性不是很强,但它得到了正确的答案

回答by Flack

As for line breaks, I myself prefer more explicit/readable way.

至于换行符,我自己更喜欢更明确/可读的方式。

<xsl:text>&#xa;</xsl:text>

回答by Chinthaka Suren Fernando

use tags it's easy

使用标签很容易

Example:
<div>TOTAL STAR POINTS EARNED </div>
<div>TOTAL STAR POINTS REDEEMED </div>
<div>BALANCE STAR POINTS AVAILABLE </div>

output:

输出:

TOTAL STAR POINTS EARNED 
TOTAL STAR POINTS REDEEMED 
BALANCE STAR POINTS AVAILABLE