我正在开发一个查看器,以使用 xslt 将 xml 日志文件显示为 html。一切都很好,除了我的本地化。生成的 HTML 文件有一个“ó”,其中一些双字节字符应该是。我无法弄清楚我做错了什么。
这是一个精简的 XSLT 文件:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/>
<xsl:variable name="language" select="nbklog/@language" />
<xsl:variable name="dictionaryName">
dictionary_<xsl:value-of select="$language"/>.xml
</xsl:variable>
<xsl:variable name="dictionary" select="document($dictionaryName)" />
<xsl:template match="/nbklog">
<html>
<body>
<h2>
<xsl:value-of select="$dictionary//String[@Key=$jobType]" />
</h2>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这是一个用于本地化的字典 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<Dictionary xml:lang="es-ES">
<String Key="Application">
Applicación
</String>
</Dictionary>
这是要转换的示例 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<nbklog id="51b654d4" jobType="backup" language="es-ES" version="1.0">
<deviceName>c:\</deviceName>
....
</nbklog>
我正在执行以下 c# 代码的转换:
string theOutputHtml;
using (MemoryStream ms = new MemoryStream()) {
using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8)) {
XPathDocument theDocument = new XPathDocument(inXmlFilename);
// Load the style sheet and run the transformation.
XslCompiledTransform theXslTrasform = new XslCompiledTransform();
theXslTrasform.Load(inXsltFilename, XsltSettings.TrustedXslt, null);
theXslTrasform.Transform(theDocument, writer);
ms.Position = 0;
using (StreamReader theReader = new StreamReader(ms)) {
theOutputHtml = theReader.ReadToEnd();
}
}
}
TheOutputHtml 的内容将有一个 'ó' 而不是 'ó'。
编辑:
在 html 字符串的 and 标记之间添加这个解决了我的问题:
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>