将 XML 转换为 HTML(而不是 xhtml)
我想将一些 xml 转换为具有以下格式的 HTML:
col1 col2 col3
注意:输出为 < code>HTML,省略可选的结束标记。这就是问题,也是问题存在的原因。
我正在使用的 XSL 片段如下:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
doctype-system='http://www.w3.org/TR/html4/strict.dtd'
doctype-public='-//W3C//DTD HTML 4.01//EN'
indent='yes'
method='html'
/>
...
<xsl:for-each select="/">
<TR><TD><xsl:value-of select="col1"/><TD><xsl:value-of select="col2"/><TD><xsl:value-of select="col3"/></TR>
</xsl:for-each>
您可以看到 XSL 的内容与我想要的 HTML 相匹配(为了便于阅读而进行了包装):
<TR> <TD><xsl:value-of select="Column1"/>
<TD><xsl:value-of select="Column2"/>
<TD><xsl:value-of select="Column3"/> </TR>
注意: 那些知道我犯的错误的人从 XSLT 获取:希望已经知道答案。
当出现我的 XSL
(别忘了,它是 xml
的一种形式)时,我得到了格式不正确的错误:
结束标记“TR”与开始标记“TD”不匹配。
这是完全有道理的。事实上:
<TD><xsl:value-of select="Column3"/> </TR>
我在关闭 TR
之前没有关闭 TD
元素。所以问题是:
鉴于 HTML
不是 xml
,如何将 xml
转换为 HTML
?
另请参阅
- HTML:包含或排除可选结束标记?
- < a href="https://stackoverflow.com/questions/2749794/xslt-transforming-into-non-xml-content">XSLT:转换为非 xml 内容?
- 省略html的可选标签
更新一个
它有有人建议,无论如何,人们可以简单地包含结束标签,以便使 XSL 验证(显示为包装)为了便于阅读):
<TR> <TD><xsl:value-of select="col1"/></TD>
<TD><xsl:value-of select="col2"/></TD>
<TD><xsl:value-of select="col3"/></TD> </TR>
然后,通过使用 xsl:output method='html'
,最终的 HTML 内容将神奇地省略 标记。除非它不起作用:
<TR><TD>col1</TD><TD>col2</TD><TD>col3</TD></TR>
更新二
有人建议我放弃,不要费心问这个问题,只需包含可选的结束标签即可。这是可能的,但这不是我的问题。此外,“解决方案”不适用于结束标记禁止的元素,例如:
<BR/>
或
<BR></BR>
我如何包含
元素,因为 HTML 中禁止关闭
元素。
i want to transform some xml into HTML that has the following format:
<TR><TD> col1 <TD> col2 <TD> col3 </TR>
Note: The output is HTML
, complete with optional closing tags omitted. This is the problem, and the reason the question exists.
A snippet of the XSL i'm using is:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
doctype-system='http://www.w3.org/TR/html4/strict.dtd'
doctype-public='-//W3C//DTD HTML 4.01//EN'
indent='yes'
method='html'
/>
...
<xsl:for-each select="/">
<TR><TD><xsl:value-of select="col1"/><TD><xsl:value-of select="col2"/><TD><xsl:value-of select="col3"/></TR>
</xsl:for-each>
You can see that the guts of the XSL matches my desired HTML (wrapped for easy reading):
<TR> <TD><xsl:value-of select="Column1"/>
<TD><xsl:value-of select="Column2"/>
<TD><xsl:value-of select="Column3"/> </TR>
Note: Those of you who know the error i'm getting from the XSLT: hopefully already know the answer.
When presented with my XSL
(which, don't forget, is a form of xml
), i get the non-well formed error:
End tag 'TR' does not match the start tag 'TD'.
This makes perfect sense. Indeed:
<TD><xsl:value-of select="Column3"/> </TR>
i do not close the TD
element before closing the TR
. So the question is:
How can i transform xml
into HTML
, given that HTML
is not xml
?
See also
- HTML: Include, or exclude, optional closing tags?
- XSLT: Transforming into non-xml content?
- Omitting optional tags of html
Update one
It has been suggested that one could simply include the closing tags anyway, in order to make the XSL validate (shown wrapped for easy reading):
<TR> <TD><xsl:value-of select="col1"/></TD>
<TD><xsl:value-of select="col2"/></TD>
<TD><xsl:value-of select="col3"/></TD> </TR>
then, by using xsl:output method='html'
, the final HTML content would have the </TD>
tags magically omitted. Except it doesn't work:
<TR><TD>col1</TD><TD>col2</TD><TD>col3</TD></TR>
Update two
It has been suggested that i give up, don't bother asking this question, and just include the optional closing tags. That's possible, but that's not my question. Also, the "solution" doesn't work for elements where the closing tag is forbidden, e.g.:
<BR/>
or
<BR></BR>
How would i include a <BR>
element in my HTML output, given that it is forbidden in HTML to close a <BR>
element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是执行此操作的一种方法:
当此转换应用于以下 XML 文档时:
正确生成所需的结果:
Here's one way to do this:
When this transformation is applied on the following XML document:
the wanted result is correctly produced:
我相信最简单的事情就是接受输出中将有结束标签。虽然它们可能是可选的,但我相信大多数人都会同意最佳实践是包含它们。
您确实不想在输出中使用可选的结束标签吗?
重新更新二
这次更新没有问题。使用
method="html"
将输出为
:XSLT(注意
< BR/>):
输入:
输出(注意
):I believe the simplest thing is to just accept you're going to have closing tags in the output. While they might be optional, I believe most people would agree that best practice is to include them.
Is there a reason you really don't want optional closing tags in the output?
Re Update Two
There's no problem with this update. With
method="html"
<BR/>
will be output as<BR>
:XSLT (note
<BR/>
):Input:
Output (note
<BR>
):您是否尝试过输出为“HTML”? HTML 中不应自闭合的元素则不会(即
、)。
如果您仍然不喜欢 XSLT 引擎序列化 HTML 输出的方式,那么您可以设置您的 < code>并构造您想要的“HTML”:
它会生成:
Have you tried the output as "HTML"? Elements that shouldn't be self-closed in HTML are not (i.e.
<BR>
,<img>
).If you still don't like how the XSLT engine is serializing HTML output, then you could set your
<xsl:output method="text">
and construct the "HTML" that you want:which produces:
Ian,您尝试过
吗?http://www.w3schools.com/xsl/el_output.asp
Ian, have you tried
<xsl:output method="text">
?http://www.w3schools.com/xsl/el_output.asp