Why does XslCompiledTransform add a META tag to HTML output?

I use this code to convert XML to HTML using an 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>... 

The result is correct, however it contains the META tag. How to disable conversion so that it does not generate a META label?

+4
source share
4 answers

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

If there is a HEAD element, then the html output method should add the META element immediately after the start tag of the HEAD element with the character encoding specified.

So, XslCompiledTransform does what the XSLT processor should do. If you do not need a meta element, you will need to explain in more detail what type of output you want exactly or why a meta is a problem if you want to get an html output. Of course, you can use the output = "xml" method, so you won’t get the meta element, but I’m not sure that the serialization result will be what you want for things like β€œbr” node nodes.

+4
source

Short answer :

Using

 <xsl:output method="xml"/> 

This excludes any added HTML tags as <meta> .

At the same time, you may have difficulty achieving the desired lexical representation of some elements.

In XSLT 2.0, you can use :

 <xsl:output method="xhtml"/> 
+5
source

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

  <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"/> 
+1
source

if you want the html output not to add the MATE tag, just add the xml namespace to the html tag like this

 <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
0
source

All Articles