使用 XSLT 解析 XML 字符串 [英] Parsing XML string using XSLT

查看:32
本文介绍了使用 XSLT 解析 XML 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 HTML 代码的 TextBlock 的 XML 文档.

I have an XML document that has a TextBlock that contains HTML code.

<TextBlock>
  <h1>This is a header.</h1>
  <p>This is a paragraph.</p>
</TextBlock>

然而,在实际的 XML 中,它是这样编码的:

In the actual XML, however, it is coded like this:

<TextBlock>
  &lt;h1&gt;This is a header.&lt;/h1&gt;
  &lt;p&gt;This is a paragraph.&lt;/p&gt;
</TextBlock>

因此,当我使用 <xsl:value-of select="TextBlock"/> 时,它会显示页面上的所有编码.有没有办法使用 XSLT 在 TextBlock 元素中将 &lt; 转换为 < ?

So when I use <xsl:value-of select="TextBlock"/> it displays all of the coding on the page. Is there a way using XSLT to convert &lt; to < within the TextBlock element?

推荐答案

<xsl:value-of select="TextBlock" disable-output-escaping="yes"/>

结果:

<h1>This is a header.</h1>
<p>This is a paragraph.</p>

Firefox 有一个相应的错误:https://bugzilla.mozilla.org/show_bug.cgi?id=98168,里面有很多评论,读起来很有趣.

Firefox has a corresponding bug: https://bugzilla.mozilla.org/show_bug.cgi?id=98168, which contains a lot of comments and is an interesting reading.

我现在正在寻找修复方法.

I am looking for a fix now.

编辑

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="disable-output-escaping.xsl"/> 
    <!-- https://bug98168.bugzilla.mozilla.org/attachment.cgi?id=434081 -->
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/TextBlock">
        <xsl:copy>
            <xsl:call-template name="disable-output-escaping"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

通过 Firebug 检查时,结果看起来是正确的:

When inspecting via Firebug, the result looks correct:

<textblock>
    <h1>This is a header.</h1>
    <p>This is a paragraph.</p>
</textblock>

这篇关于使用 XSLT 解析 XML 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆