如何转换全部LF
字符到<br />
标签并显示在 HTML 页面上?
我有以下示例 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="data.xslt"?>
<data>
<lines>
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
</lines>
</data>
我想显示 HTML 页面上的所有行。为此,我使用以下 XSLT 转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<xsl:variable name="filtered">
<xsl:call-template name="replace">
<xsl:with-param name="string" select="./data/lines"/>
<xsl:with-param name="search" select="'
'"/>
<xsl:with-param name="new"><br /></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<td align="left">
<xsl:value-of select="$filtered" disable-output-escaping="yes"/>
</td>
</body>
</html>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="string"/>
<xsl:param name="search"/>
<xsl:param name="new"/>
<xsl:choose>
<xsl:when test="contains($string, $search)">
<xsl:value-of select="substring-before($string, $search)"/>
<xsl:value-of select="$new"/>
<xsl:call-template name="replace">
<xsl:with-param name="string" select="substring-after($string, $search)"/>
<xsl:with-param name="search" select="$search"/>
<xsl:with-param name="new" select="$new"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当我在 Firefox 中打开该 XML 文件(我使用浏览器显示 XSLT 转换)时,我将看到该结果:
Line 1Line 2Line 3Line 4Line 5Line 6
如您所见,LF
字符未被 <br />
替换标签。
但是当我使用其他字符串时,例如 EOL
:
<xsl:with-param name="new">EOL</xsl:with-param>
我会看到预期的结果:
EOLLine 1EOLLine 2EOLLine 3EOLLine 4EOLLine 5EOLLine 6EOL
问题在于转换/显示 <br />
标签。
最佳答案
您可以将节点片段作为参数值传递,就像 <xsl:with-param name="new"><br /></xsl:with-param>
一样。 ,但将其输出为 br
您需要使用模板中的元素 <xsl:copy-of select="$new"/>
,不是xsl:value-of
.
[编辑] 这是一个示例:http://home.arcor.de/martin.honnen/xslt/test2012062801.xml 。样式表位于 http://home.arcor.de/martin.honnen/xslt/test2012062801.xsl ,我还将其包含在下面:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.01" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Example</title>
</head>
<body>
<xsl:variable name="filtered">
<xsl:call-template name="replace">
<xsl:with-param name="string" select="data/lines"/>
<xsl:with-param name="search" select="'
'"/>
<xsl:with-param name="new"><br /></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<div>
<xsl:copy-of select="$filtered"/>
</div>
</body>
</html>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="string"/>
<xsl:param name="search"/>
<xsl:param name="new"/>
<xsl:choose>
<xsl:when test="contains($string, $search)">
<xsl:value-of select="substring-before($string, $search)"/>
<xsl:copy-of select="$new"/>
<xsl:call-template name="replace">
<xsl:with-param name="string" select="substring-after($string, $search)"/>
<xsl:with-param name="search" select="$search"/>
<xsl:with-param name="new" select="$new"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
关于html - 如何将所有 "LF"字符转换为 "<br/>"标签并显示在HTML页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11222334/