Parsing XML string using XSLT

我是研究僧i 提交于 2019-12-18 09:17:49

问题


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>

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>

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?


回答1:


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

and the result:

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

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.

EDIT

<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>

When inspecting via Firebug, the result looks correct:

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


来源:https://stackoverflow.com/questions/4492303/parsing-xml-string-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!