我想为XSLT中的文本输出生成换行符。什么好主意吗?
当前回答
我不能只使用<xsl:text>
</xsl:text>方法,因为如果使用XSLT格式化XML文件,实体就会消失。所以我不得不使用一种稍微迂回的方法来使用变量
<xsl:variable name="nl" select="' '"/>
<xsl:template match="/">
<xsl:value-of select="$nl" disable-output-escaping="no"/>
<xsl:apply-templates select="*"/>
</xsl:template>
其他回答
<xsl:text xml:space="preserve">
</xsl:text>
我不能只使用<xsl:text>
</xsl:text>方法,因为如果使用XSLT格式化XML文件,实体就会消失。所以我不得不使用一种稍微迂回的方法来使用变量
<xsl:variable name="nl" select="' '"/>
<xsl:template match="/">
<xsl:value-of select="$nl" disable-output-escaping="no"/>
<xsl:apply-templates select="*"/>
</xsl:template>
只需添加这个标签:
<br/>
这对我有用;)。
你可以试试,
<xsl:text>
</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的挫折经验;)