2025-03-08 09:00:04

在XSLT中生成新行

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


当前回答

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

<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: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>

我最喜欢的方法如下:

<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>&#xA;</xsl:text>

它会起作用的。