Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

decoding - Parsing XML string using XSLT

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...