Why does XslCompiledTransform add META tag to HTML

2019-06-24 00:58发布

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>...

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

标签: .net xslt
4条回答
来,给爷笑一个
2楼-- · 2019-06-24 01:36

Short answer:

Use:

<xsl:output method="xml"/>

This eliminates any added HTML tags susch as <meta>.

At the same time, you may have difficulties achieving the exact wanted lexical representation of some elements.

In XSLT 2.0 one can use:

<xsl:output method="xhtml"/>
查看更多
别忘想泡老子
3楼-- · 2019-06-24 01:36

if you want the html output without add a MATE tag just add a xml namespace in html tag like this

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
查看更多
何必那么认真
4楼-- · 2019-06-24 01:38

It also depends upon the doctype you insert from your output tag. Using XHTML, for example, omits the META tag.

   <xsl:output method="html"
               doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
               doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
查看更多
啃猪蹄的小仙女
5楼-- · 2019-06-24 01:45

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:

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.

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.

查看更多
登录 后发表回答