如何使用xslt添加WhiteSpace?

时间:2010-05-17 14:57:40

标签: xslt

如果我尝试使用<xsl:text>&#160;</xsl:text>在我的xslt代码中添加空格,则在转换为html后会显示一个问号。请帮助解决这个问题。

由于 普拉迪普

1 个答案:

答案 0 :(得分:1)

您可能会在以下代码中明确指定编码:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" encoding="utf-8"/>
 <xsl:template match="node()|@*">
   <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="td[not(node())]">
  <xsl:copy>
   <xsl:text>&#160;</xsl:text>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <html>
   <head>
   </head>
   <xsl:apply-templates/>
  </html>
 </xsl:template>
</xsl:stylesheet>

对此XML文档应用此转换时

<table border="1">
<tr>
 <td>X</td>
 <td></td>
 <td>X</td>
</tr>
</table>

生成所需结果,浏览器(IE8)正确显示硬空间

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<table border="1">
<tr>
<td>X</td>
<td> </td>
<td>X</td>
</tr>
</table>
</html>
相关问题