【发布时间】:2019-02-22 04:32:08
【问题描述】:
我一直在做很多工作,试图弄清楚如何使用样式表查看 XML 文件(在 Internet Explorer 上),并保留文档中的回车符/换行符。
我确实让它工作了,但不想在我的 XML 文件中使用大量带有 [br/] 的 CDATA。我希望在 XML 文本文件中使用保存的回车。
我看到了其他示例,例如:how to convert NEWLINE into <BR/> with XSLT?,但我不太擅长 XML/XSL,不知道如何使其正常工作。我所做的一切都放置了一个“<br/>”或 CR/LF 但不是浏览器可以理解的
。
XSL 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body style="font-family:verdana;">
<h2>Example</h2>
<table border="1" bordercolor="#000000" cellspacing="0">
<tr bgcolor="#000000" style="color:#FFFFFF;text-align:left;font-size:80%">
<th>#</th>
<th>Has <![CDATA["CDATA[<br/>]"]]> (It Works)</th>
<th>Has <![CDATA["
"]]> (Want this one as worst case)</th>
<th>Has Carriage Return (Want this one to work)</th>
</tr>
<xsl:for-each select="report/test">
<tr style="text-align:left;font-size:80%">
<xsl:choose>
<xsl:when test="@type = 'append_text'">
<td><b><xsl:value-of select="text"/></b></td>
</xsl:when>
<xsl:when test="@type = 'test_step'">
<td id="ref{num}"><xsl:value-of select="num"/></td>
<td><xsl:value-of select="hasBR" disable-output-escaping="yes"/></td>
<td><xsl:value-of select="hasXA" disable-output-escaping="yes"/></td>
<td><xsl:value-of select="hasCR" disable-output-escaping="yes"/></td>
</xsl:when>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="51380-200.xsl"?>
<report>
<test type="test_step">
<num>1</num>
<hasXA>Line 1 
 Line 2 
 Line 3</hasXA>
<hasBR>Line 1 <![CDATA[<br/>]]> Line 2 <![CDATA[<br/>]]> Line 3</hasBR>
<hasCR>Line 1
Line2
Line3</hasCR>
</test>
<test type="test_step">
<num>2</num>
<hasXA>Line 1 
 Line 2 
 Line 3</hasXA>
<hasBR>Line 1 <![CDATA[<br/>]]> Line 2 <![CDATA[<br/>]]> Line 3</hasBR>
<hasCR>Line 1
Line2
Line3</hasCR>
</test>
<test type="test_step">
<num>3</num>
<hasXA>Line 1 
 Line 2 
 Line 3</hasXA>
<hasBR>Line 1 <![CDATA[<br/>]]> Line 2 <![CDATA[<br/>]]> Line 3</hasBR>
<hasCR>Line 1
Line2
Line3</hasCR>
</test>
</report>
非常感谢您的帮助!
注意,我一直在使用 XSL transform.net 来尝试使其工作。我在上面加载了它的一个版本。 http://xsltransform.net/bESZULX
【问题讨论】:
-
不是有效的 XML,但
是 .我在整个程序中使用
没有问题。