XSLT self-closing tags issue

后端 未结 12 873
遥遥无期
遥遥无期 2020-12-08 20:19

I am using xslt to transform an xml file to html. The .net xslt engine keeps serving me self-closing tags for empty tags.

Example:

12条回答
  •  有刺的猬
    2020-12-08 21:08

    There are a few things you need to be careful:

    1. In your xsl use < xsl:output method='html'>
    2. set OutputSettings in your output XmlWriter
    3. in the Html inside your xsl, don't set attributes in html tag like this < html xmlns="http://www.w3.org/1999/xhtml"> but use < html> instead.

    This is a piece of working code:

    string xmlStr = "";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlStr);
    string xslContents = @"
    
    
    
    
    
        
    "; XslCompiledTransform xsl = new XslCompiledTransform(); xsl.Load(XmlReader.Create(new StringReader(xslContents))); StringWriter result = new StringWriter(); using (XmlWriter writer = XmlWriter.Create(result, xsl.OutputSettings)) { xsl.Transform(doc, null, writer); } System.Diagnostics.Debug.Write( result.ToString());

提交回复
热议问题