xml - 说服 Firefox 解析 XML 中嵌入的 HTML(应用 XSL 后)

标签 xml firefox xslt

我有一些这种形式的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="biomirror.xsl"?>
<Thread>
    <Title> Some thread title </Title>
    <Posts>
        <Post>
            <Author> Me </Author>
            <Body>
                This is the post body, which <b>may</b> have embedded XHTML, including all sorts of things like:<br />
                <div class="quote">Quotes</div>
                I know it's XHTML, though, the program spitting out XML verifies that.
            </Body>
        </Post>
    </Posts>
</Thread>

我需要将它们格式化为可读的线程,因此我使用 CSS 样式表和 XSL 样式表。 CSS 有效,我知道这没有任何问题。我的问题似乎出在 XSL 上,因为 Firefox 不会解析任何嵌入的 XHTML。在 IE 中,它工作得很好,并且具有正确的格式,但在 Firefox 中,它完全是纯文本。我认为这与它在输出之前被转义有关,但我不知道如何防止这种情况。

XSL 是:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 
  omit-xml-declaration="yes" 
  method="xml" 
  media-type="application/xhtml+xml" 
  indent="no" 
  doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
/>
<xsl:template match="Posts">
<xsl:for-each select="Post">
    <tr xmlns="http://www.w3.org/1999/xhtml" class="Post">
      <td>
        <div>
          <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
              <td class="Author">
                <xsl:value-of select="Author"/>
              </td>
              <td class="Date">
                Post <xsl:value-of select="PostID"/> 
                <xsl:choose>
                  <xsl:when test="count(LastPost) > 0">
                    (lastpost)
                  </xsl:when>
                </xsl:choose> at <xsl:value-of select="Date"/>
              </td>
            </tr>
          </table>
        </div>
        <div class="Body">
           <xsl:copy-of select="Body" />
        </div>
        <xsl:choose>
          <xsl:when test="count(Sig) = 1">
            <div class="Sig">
              <xsl:value-of disable-output-escaping="yes" select="Sig"/>
            </div>
          </xsl:when>
          <xsl:when test="count(Sig) = 0">
            <div class="SigFooter"> </div>
          </xsl:when>
        </xsl:choose>
      </td>
    </tr>
</xsl:for-each>
</xsl:template>

<xsl:template match="Thread">
  <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <xsl:choose>
          <xsl:when test="count(Title) = 1">
            <title>
              <xsl:value-of select="Title"/>
            </title>
          </xsl:when>
        </xsl:choose>
        <link href="resources/main.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
      <table class="Thread" align="center" width="90%" height="95%" cellpadding="2em">
        <tr>
          <td colspan="3">
            <div class="Title">
          <xsl:value-of select="Title"/>
              <br />
              <a href="whatis.xml">
                <img src="resources/banner.png" />
              </a>
            </div>
          </td>
        </tr>
        <xsl:apply-templates select="Posts"/>
        <tr height="100%">
         <td valign="bottom">
       <div class="Footer">
             Footer message n stuff
           </div>
         </td>
        </tr>
      </table>
    </body>
  </html>
</xsl:template>

这有点像黑客攻击,任何奇怪的属性我都归咎于 Visual Studio。这是我第一次使用 XSL(不过对 XML 比较熟悉),所以我不知道如何解决这个问题。 :)

现在,我读到了这个问题: XSLT: Parsing HTML embedded in XML? 并尝试将其集成到我的 XML 和 XSL 中(如上所示)。但在 Firefox 中仍然不起作用。

编辑:另外,我尝试了 xsl:value-of 和 xsl:copy-of 来输出内容。 Value-of 输出纯文本并尊重我的格式(来自 CSS),copy-of 输出纯文本并破坏我的格式(返回正文格式,忽略 div 和表格)。

编辑2:修改了 XSL 以反射(reflect)答案中的建议。格式很好,但嵌入的标签仍然以文本形式显示,没有被解释。

最佳答案

首先,您不需要这些声明:

xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"

删除它们是安全的。

其次,你永远不需要提及 child::轴。这在 XSLT 中是隐含的,只需将其踢出而不进行替换。

第三,移动XHTML命名空间声明

xmlns="http://www.w3.org/1999/xhtml"

<xsl:stylesheet>元素并在其他地方删除它。

最后但并非最不重要的一点:您是否尝试过正确的 XHTML 输出方法和媒体类型?

<xsl:output method="xml" media-type="application/xhtml+xml">

关于xml - 说服 Firefox 解析 XML 中嵌入的 HTML(应用 XSL 后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3268827/

相关文章:

javascript - Nativescript:滚动时重用 ListView 项目

html - p 标签的 CSS 类在 Chrome 和 Safari 中有效,但被 Firefox 忽略

javascript - Firefox 版本 : chrome. tabs.onUpdated.addListener

Css 位置,与 Web-kit 和 FF 略有不同

xml - 如何在 XSLT 中呈现带有属性的自闭合 HTML 标签?

xml - 逻辑应用 - XML 转换 - 禁止执行 'document()' 函数?

xml - 根据元素值改变 XSD 中的类型?

c# - 使用 LINQ 查询 XML 文件

java - 使用 JDK 1.6 时,我的 Web 应用程序中是否需要 stax-api-1.0.x?

javascript - 如何在 XSLT 中包含 CSS 文件