-1

有没有办法在 XPath 表达式中添加换行符?

这是我当前的表达式,它不起作用

{concat(@Parameter1, $NullObj, @Parameter2)}

其中变量$NullObj等于
LINEFEED 字符的十六进制值。

这输出为“Parameter1 Parameter2”。

编辑:不幸的是,我使用的是 Xpath / Xsl 1.0。

编辑:下面的示例代码。XML:

<Post>
  <Title>Title Text</Title>
  <Body>Body Text </Body>
  <Author>Author Name</Author>
</Post>

示例 XSL

<xsl:Variable name="NullObj">
<xsl:value-of select="'&#xa;'" />
</xsl:variable>

Edit2:完整的 Xpath 上下文,根据要求:

<xsl:value-of select="concat("The title is", @Title, $NullObj, "The Body reads:", @Body, $NullObj, "The Author is", @Author)" />

结果输出不包括换行符。我错过了一些基本的东西吗?我也试过用 替换&#xa;&#10;但没有运气。谢谢你的耐心。

4

2 回答 2

1

演示最小、完整和可验证示例的含义:

以下样式表:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="NullObj">
    <xsl:value-of select="'&#xa;'" />
</xsl:variable>

<xsl:template match="/Post">
    <xsl:copy>
        <xsl:value-of select="concat('The title is: ', Title, $NullObj, 'The Body reads:', Body, $NullObj, 'The Author is: ', Author)" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入,返回:

<?xml version="1.0" encoding="UTF-8"?>
<Post>The title is: Title Text
The Body reads:Body Text 
The Author is: Author Name</Post>

如您所见,换行符是您期望的位置。


编辑:

显然,您正在尝试生成HTML 输出- 所以正确的答案是:

<xsl:value-of select="concat('The title is: ', Title)" />
<br/>
<xsl:value-of select="concat('The Body reads:', Body)" />
<br/>
<xsl:value-of select="concat('The Author is: ', Author)" />
于 2016-05-04T19:01:34.883 回答
0

解决了。这个问题原来是一个空白问题。显然将其设置为“pre”以外的任何内容都会忽略 IE 中的 Xpath 换行符。

于 2016-05-05T14:29:42.867 回答