Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does XslCompiledTransform add META tag to HTML output?

Tags:

.net

xslt

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?

like image 968
jlp Avatar asked Dec 28 '22 04:12

jlp


2 Answers

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"/>
like image 57
Dimitre Novatchev Avatar answered Jan 11 '23 22:01

Dimitre Novatchev


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.

like image 33
Martin Honnen Avatar answered Jan 11 '23 22:01

Martin Honnen