2025-03-08 09:00:04

在XSLT中生成新行

我想为XSLT中的文本输出生成换行符。什么好主意吗?


在xsl:output标记中包含属性Method="text",并在xsl中适当的位置在文本内容中包含换行符。如果您希望XSL源代码保持整洁,请使用实体
你想要一条新线。


我最喜欢的方法如下:

<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代码将产生换行符(换行符):

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

对于回车,使用:

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

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

参见示例

<xsl:variable name="module-info">
  <xsl:value-of select="@name" /> = <xsl:value-of select="@rev" />
  <xsl:text>&#10;</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:variable name='nl'><xsl:text>
</xsl:text></xsl:variable>

然而,我一直在使用Ant任务<echoxml>到 创建样式表并针对文件运行它们。的 task将做属性值模板,例如${DSTAMP}, 但它也会重新格式化xml,所以 在这种情况下,最好使用实体引用。

<xsl:variable name='nl'><xsl:text>&#xa;</xsl:text></xsl:variable>

只需添加这个标签:

<br/>

这对我有用;)。


我发现了<xsl:text>中的文字换行符和使用&#xA;的文字换行符之间的区别。

虽然文字换行符在我的环境中工作得很好(使用Saxon和默认的Java XSLT处理器),但当在. net环境中运行的另一个组执行代码时,我的代码失败了。

更改为实体(&#xA;)使我的文件生成代码在Java和. net上一致地运行。

此外,文字换行符很容易被ide重新格式化,当文件由“不知情”的人维护时,可能会无意中丢失。


我在这里添加了DOCTYPE指令:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nl "&#xa;">
]>
<xsl:stylesheet xmlns:x="http://www.w3.org/2005/02/query-test-XQTSCatalog"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

这允许我使用&nl;而不是&#xa;在输出中产生换行符。与其他解决方案一样,这通常放在<xsl:text>标记中。


根据我的经验,我注意到在<xsl:variable>子句内生成新行是行不通的。 我想做的是:

<xsl:variable name="myVar">
  <xsl:choose>
    <xsl:when test="@myValue != ''">
      <xsl:text>My value: </xsl:text>
      <xsl:value-of select="@myValue" />
      <xsl:text></xsl:text> <!--NEW LINE-->
      <xsl:text>My other value: </xsl:text>
      <xsl:value-of select="@myOtherValue" />
    </xsl:when>
  </xsl:choose>
<xsl:variable>

<div>
  <xsl:value-of select="$myVar"/>
</div>

我试图在“新行”(空的<xsl:text>节点)中放入的任何东西都不起作用(包括本页中大多数更简单的建议),更不用说HTML在那里不起作用的事实,所以最终我不得不将其分割为2个变量,在<xsl:variable>作用域之外调用它们,并在它们之间放置一个简单的<br/>,即:

<xsl:variable name="myVar1">
  <xsl:choose>
    <xsl:when test="@myValue != ''">
      <xsl:text>My value: </xsl:text>
      <xsl:value-of select="@myValue" />
    </xsl:when>
  </xsl:choose>
<xsl:variable>

<xsl:variable name="myVar2">
  <xsl:choose>
    <xsl:when test="@myValue != ''">
      <xsl:text>My other value: </xsl:text>
      <xsl:value-of select="@myOtherValue" />
    </xsl:when>
  </xsl:choose>
<xsl:variable>

<div>
  <xsl:value-of select="$myVar1"/>
  <br/>
  <xsl:value-of select="$myVar2"/>
</div>

是的,我知道,这不是最复杂的解决方案,但它是有效的,只是分享我对xsl的挫折经验;)


我不能只使用<xsl:text>&#xa;</xsl:text>方法,因为如果使用XSLT格式化XML文件,实体就会消失。所以我不得不使用一种稍微迂回的方法来使用变量

<xsl:variable name="nl" select="'&#10;'"/>
<xsl:template match="/">
    <xsl:value-of select="$nl" disable-output-escaping="no"/>
    <xsl:apply-templates select="*"/>
</xsl:template>

恕我直言,除了@Florjon提供的信息,我们不需要更多的信息。也许会留下一些小细节来理解为什么它有时不适合我们。

首先,<xsl:text/>中的&#xa (hex)或&#10 (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>&lt;!DOCTYPE Subscriptions SYSTEM "Subscriptions.dtd"&gt;&#xa;&#xa;&#xd;</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"?>
&lt;!DOCTYPE Subscriptions SYSTEM "Subscriptions.dtd"&gt;

&#13;<Subscriptions>
    <User id="1"/>   
</Subscriptions>

好的,它做了我们期望的,转义,这样我们使用的字符就能正确显示。根节点内的XML部分格式化由ident="yes"处理。但是仔细看看,我们会发现换行符&#xa并没有转义和翻译,而是执行了双换行!我对此没有解释,知道就好了。有人知道吗?

第二个版本没有脱离角色,所以他们正在制作他们想要的东西。所作的更改如下:

<xsl:text disable-output-escaping="yes">&lt;!DOCTYPE Subscriptions SYSTEM "Subscriptions.dtd"&gt;&#xa;&#xa;&#xd;</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:&#xd,而输出xml是由DOM正确验证的。

我正在查看一个损坏的xml:

<?xml version="1.0" encoding="utf-8"?>
<Subscriptions>riptions SYSTEM "Subscriptions.dtd">
    <User id="1"/>   
</Subscriptions>

DOM解析器会忽略控制字符,但呈现的控件不会。我花了很长时间撞了撞头,才意识到我没有看到这个是多么愚蠢!

为了记录,我确实在两个CRLF的体内使用了一个变量,只是为了100%确定它在任何地方都可以工作。


你可以试试,

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

它会起作用的。


<xsl:text xml:space="preserve">
</xsl:text>