我试图在将 xml 文件转换为 html 时保留行中断,但我找不到可行的方法。
<meta>
<name>Message</name>
<value>Hi!
I need info!
Mr Test</value>
</meta>
我使用这个 xsl:
<xsl:if test="name='Message'">
<tr>
<th align="left" colspan="2">Message:</th>
</tr>
<tr>
<td colspan="2"><xsl:value-of select="value"/></td>
</tr>
</xsl:if>
但是新行 (cr/lf) 字符消失了,一切都变成了 html 中的一行。是否可以匹配 cr/lf 并将它们替换为 html“<_br >”或任何其他方法?
最佳答案
将以下模板添加到您的 XSL 中:-
<xsl:template name="LFsToBRs">
<xsl:param name="input" />
<xsl:choose>
<xsl:when test="contains($input, ' ')">
<xsl:value-of select="substring-before($input, ' ')" /><br />
<xsl:call-template name="LFsToBRs">
<xsl:with-param name="input" select="substring-after($input, ' ')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
现在用调用此模板替换 where select 值:-
<td colspan="2">
<xsl:call-template name="LFsToBRs">
<xsl:with-param name="input" select="value"/>
</xsl:call-template>
</td>
关于html - 如何转换xml并保留换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1873449/