.net XSLT 自闭合标签问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/887739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:43:59  来源:igfitidea点击:

XSLT self-closing tags issue

.netxslttagsxslcompiledtransform

提问by

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

我正在使用 xslt 将 xml 文件转换为 html。.net xslt 引擎不断为我提供空标签的自闭合标签。

Example:

例子:

<div class="test"></div> 

becomes

变成

<div class="test" />

The former is valid html, while the latter is illegal html and renders badly. My question is : How do I tell the xslt engine (XslCompiledTransform) to not use self-closing tags.

前者是有效的 html,而后者是非法的 html 并且呈现很糟糕。我的问题是:如何告诉 xslt 引擎 (XslCompiledTransform) 不使用自闭合标签。

If it's not possible, how can I tell my browser (IE6+ in this case) to interpret self-closing tags correctly.

如果不可能,我如何告诉我的浏览器(在本例中为 IE6+)正确解释自关闭标签。

采纳答案by catalpa

If you are using XmlWriter as your ouput stream, use HTMLTextWriter instead. XMLWriter will reformat your HTML output back to XML.

如果您使用 XmlWriter 作为输出流,请改用 HTMLTextWriter。XMLWriter 会将您的 HTML 输出重新格式化为 XML。

回答by Harry Lime

Change your xsl:outputmethod to be html(instead of xml).

将您的xsl:output方法更改为html(而不是xml)。

Or add it if you haven't already got the element

或者如果您还没有获得元素,请添加它

<xsl:output method="html"/>

回答by Netsi1964

A workaround can be to insert a comment element to force generation of non self closing:

解决方法可以是插入注释元素以强制生成非自关闭:

<script type="text/javascript" src="nowhere.js">
<xsl:comment></xsl:comment>
</script>

It is not a pretty soloution, but it works :-)

这不是一个很好的解决方案,但它有效:-)

/Sten

/斯坦

回答by Yajo

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

对我来说,这是脚本标签中的一个问题。我通过用分号 ( ;)填充它来解决它

<script type="text/javascript" src="somewhere.js">;</script>

回答by kernel

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

我曾经在<xsl:text>里面放一个元素,比如:

<script type="text/javascript" src="/scripts/jquery.js"><xsl:text> </xsl:text></script>

回答by Steven Huwig

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

你不能告诉你的浏览器把无效的 HTML 作为 HTML 处理——你很幸运它完全理解格式错误的 HTML。:)

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.

但是,如果您的源文档具有名称空间,这将不起作用。如果名称空间节点出现在输出中,XSLT 处理器似乎会默默地将输出方法改回 XML。

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:copy-of><xsl:copy>创建元素,例如

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

See

etc.

等等。

回答by Renzo Ciot

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.
  1. 在您的 xsl 中使用 < xsl:output method='html'>
  2. 在输出 XmlWriter 中设置 OutputSettings
  3. 在你的 xsl 中的 Html 中,不要像这样在 html 标签中设置属性 < html xmlns="http://www.w3.org/1999/xhtml"> 而是使用 < html> 。

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());

回答by gblmarquez

The easy way I found was creating a new XmlTextWriter class to override the method WriteEndElement, forcing the non-closing tag and pass on the serialization process as parameter.

我发现的简单方法是创建一个新的 XmlTextWriter 类来覆盖方法 WriteEndElement,强制使用非结束标记并将序列化过程作为参数传递。

public class MyXmlTextWriter : XmlTextWriter
{
    public MyXmlTextWriter(Stream stream) : base(stream, Encoding.UTF8)
    { }
    public MyXmlTextWriter(TextWriter stream) : base(stream)
    { }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }
}

回答by Ilya Kharlamov

Don't try this at home:

不要在家里尝试这个:

<xsl:when test="self::* and not(text())">
    <xsl:value-of select="concat('&lt;', name(), '&gt;', '&lt;/', name(), '&gt;')" disable-output-escaping="yes"/>
</xsl:when>