XSLT self-closing tags issue

后端 未结 12 875
遥遥无期
遥遥无期 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:00

    For me it was a problem in the script tag. I solved it by filling it with a semicolon (;)

    <script type="text/javascript" src="somewhere.js">;</script>
    
    0 讨论(0)
  • 2020-12-08 21:02

    Just experienced the same issue with PHP 5's XSL, with output/@method=html. Seems that assigning an empty value attribute will cause elements to be output as invalid non-self-closing, non-closed tags:

    <input type="text" name="foo" value="{my-empty-value}" />
    

    results in:

    <input type="text" name="foo" value="">
    

    One possible solution is to conditionally add the attribute:

    <xsl:if test="string-length(my-empty-value) > 0">
        <xsl:attribute name="value">
            <xsl:value-of select="my-empty-value" />
        </xsl:attribute>
    </xsl:if>
    

    resulting in:

    <input type="text" name="foo" />
    
    0 讨论(0)
  • 2020-12-08 21:03

    I used to put an <xsl:text> element inside, like:

    <script type="text/javascript" src="/scripts/jquery.js"><xsl:text> </xsl:text></script>
    
    0 讨论(0)
  • 2020-12-08 21:04

    You can't tell your browser to handle invalid HTML as HTML -- you're lucky it understands malformed HTML at all. :)

    Definitely do this in your stylesheet:

    <xsl:output method="html"/>
    

    But, if your source document has namespaces, this won't do the trick. XSLT processors seem to silently change the output method back to XML if namespace nodes are present in the output.

    You need to replace all instances of <xsl:copy-of> and <xsl:copy> with creations of elements with just the local name, e.g.

    <xsl:template match="*">
       <xsl:element name="{local-name()}">
         <xsl:apply-templates/>
       </xsl:element>
    </xsl:template>
    

    See

    • http://www.biglist.com/lists/xsl-list/archives/200708/msg00538.html
    • http://www.stylusstudio.com/xsllist/200509/post90860.html
    • http://www.richardhallgren.com/removing-namespace-from-outgoing-messages/

    etc.

    0 讨论(0)
  • 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 = "<?xml version='1.0' encoding='UTF-8'?><Data></Data>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlStr);
    string xslContents = @"
    <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' version='4.0' omit-xml-declaration='yes' indent='yes'/>
    <xsl:template match='Data'>
    <html>
    <body>
        <div></div>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>";
    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());
    
    0 讨论(0)
  • 2020-12-08 21:09

    This is related to the XslCompiledTransform class

    here is a workaround:

    http://blogs.msdn.com/b/nareshjoshi/archive/2009/01/15/how-to-force-non-self-closing-tags-for-empty-nodes-when-using-xslcompiledtransform-class.aspx

    0 讨论(0)
提交回复
热议问题