为什么XslCompiledTransform添加META标记HTML输出?标记、XslCompiledTransform、META、HTML

2023-09-06 23:00:35 作者:呦西大大滴

我用这个code来转换XML使用XSLT模板HTML:

I use this code to transform XML to HTML using XSLT template:

string uri = Server.MapPath("~/template.xslt");
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(uri);
XDocument xml = new XDocument(new XElement("Root"));
StringBuilder builder = new StringBuilder();
XmlReader reader = xml.CreateReader();
XmlWriter writer = XmlWriter.Create(builder, xsl.OutputSettings);
xsl.Transform(reader, writer);
writer.Close();

我的模板看起来是这样的:

My template looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"  />
<xsl:template match="Root">
    <html>
       <head>...

输出是正确的但它包含META标记。如何禁用转换,因此不会产生META标记?

Output is correct however it contains META tag. How to disable transform so it would not generate META tag?

推荐答案

在XSLT 1.0规范输出方法=HTML(http://www.w3.org/TR/xslt#section-HTML-Output-Method )强制要求元元是输出如果在结果树头部分:

The XSLT 1.0 specification for output method="html" (http://www.w3.org/TR/xslt#section-HTML-Output-Method) mandates that a meta element is output if there is a head section in the result tree:

如果有一个HEAD元素,则   HTML输出方法应该添加一个META   元件后,立即   HEAD元素的开始标记   指定的字符编码   实际使用。

If there is a HEAD element, then the html output method should add a META element immediately after the start-tag of the HEAD element specifying the character encoding actually used.

所以XslCompiledTransform做什么的XSLT处理器应该做的。如果你不希望元元,你将需要更详细地解释什么样的输出正是你想要或者为什么元是一个问题,如果你想HTML输出。当然,你可以使用输出方法=XML,这样你就不会得到meta元素,但我不知道序列化结果的方式将你想要的东西,如'BR'元素节点是什么。

So XslCompiledTransform does what an XSLT processor should do. If you don't want the meta element you will need to explain in more detail what kind of output you want exactly or why the meta is a problem if you want html output. You could of course use output method="xml", that way you won't get the meta element but I am not sure the serialization result that way will be what you want for stuff like 'br' element nodes.