使用XSL在XML文档中输出HTML标记

时间:2013-07-29 15:29:47

标签: c# html xml xslt tags

关于我的问题有大约一百万个问题,我已经实现了大部分答案而没有任何运气。

我正在编写一个包含webbrowser控件的C#应用​​程序。在表单加载时,webbrowser控件加载以下XML。这个XML使用它下面的XSL设置样式。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="messages.xsl"?>
<messages>
  <message>
    <date>25/07/2013</date>
    <time>11:41:07</time>
    <locations>Everyone</locations>
    <title>Test message 1</title>
    <body>&amp;lt;P&amp;gt;This is a test message.&amp;lt;/P&amp;gt;&amp;lt;P&amp;gt;&amp;amp;nbsp;&amp;lt;/P&amp;gt;&amp;lt;P&amp;gt;Test!&amp;lt;/P&amp;gt;</body>
    <publisher>Test</publisher>
  </message>
  <message>
    <date>29/07/2013</date>
    <time>11:20:10</time>
    <locations>Everyone</locations>
    <title>Another test message</title>
    <body>&amp;lt;P&amp;gt;This is another test message.&amp;lt;/P&amp;gt;&amp;lt;P&amp;gt;&amp;amp;nbsp;&amp;lt;/P&amp;gt;&amp;lt;P&amp;gt;blah blah&amp;lt;b&amp;gt;blah blah &amp;lt;/b&amp;gt;blah blah.&amp;lt;/P&amp;gt;</body>
    <publisher>Test</publisher>
  </message>
</messages>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html&gt;</xsl:text>
    <html>
        <head>
            <style>
                div#message {
                    margin-bottom: 5px;
                    border: 0px solid black;
                }
                div#header {
                    /*background-color: #98AFC7;*/
                    margin-bottom: 2px;
                }
                div.headline {
                    font-size: 20px;
                    font-family: Verdana;
                    cursor: hand;
                }
                div.headline:hover {
                    font-size: 20px;
                    font-family: Verdana;
                    color: orange;
                    cursor: hand;
                }
                div#tagline {
                    font-size: 9px;
                    font-family: Verdana;
                }
                div.main {
                    font-size: 12px;
                    font-family: Verdana;
                    background-color: #C3D9FF;
                    margin-top: 0px;
                    padding-bottom: 2px;
                    margin-bottom: 0px;
                }
                div#tags {
                    margin-top: 10px;
                }
                div#footer {

                }
                div#hr {
                    background-color: #4095EF;
                    height: 2px;
                    padding: 0px;
                    margin: 0px;
                }
            </style>
            <script language="javascript" type="text/javascript" src="jquery-1.10.2.min.js"></script>
            <script language="javascript" type="text/javascript">
                function toggleBody(id) {
                    $("#main_" + id + "").toggle();
                }

                $(document).ready(function() {
                    $(".main").hide();



                });
            </script>
        </head>
        <body>
            <div id="messages">
                <xsl:for-each select="messages/message">
                <xsl:variable name="i" select="position()"/>
                    <div id="message">
                        <div id="header">
                            <div id="{$i}" class="headline"><xsl:attribute name="onClick">javascript:toggleBody(this.id)</xsl:attribute>
                                <xsl:value-of select="title" disable-output-escaping="yes"/>
                            </div>
                            <div id="tagline">
                                Published on <xsl:value-of select="date"/> at <xsl:value-of select="time"/> by <xsl:value-of select="publisher"/>.
                            </div>
                        </div>
                        <div id="main_{$i}" class="main">
                            <div id="body">
                                <xsl:value-of select="body" disable-output-escaping="yes"/>
                            </div>
                            <div id="tags">
                                Tags: <xsl:value-of select="locations" />
                            </div>
                        </div>
                        <div id="footer">
                            <div id="hr">
                            </div>
                        </div>
                    </div>
                </xsl:for-each>
            </div>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

不幸的是,呈现的HTML显示如下:

<P>This is a test message.</P><P>&nbsp;</P><P>Test!</P>

<P>This is another test message.</P><P>&nbsp;</P><P>blah blah<b>blah blah </b>blah blah.</P>

我将如何正确呈现HTML标签?

我尝试过使用copy-of和disable-output-escaping =“yes”但没有成功。我已经尝试将输出更改为HTML并添加模板但是再次没有欢乐。

任何人都可以提供帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

  

你控制输入了吗?您需要一种类似&lt; P&gt;这样的格式。这是一条消息。&lt; / P&gt; (我将其标记为:

<body><![CDATA[<P>This is a message.</P>]]></body>) 
  

然后将body的字符串值设为:

<P>This is a message.</P> 
  使用disable-output-escaping的

将在结果中构造P元素。有了双重逃逸,你就不能指望&amp; amp; lt; P&amp; amp; gt;创建一个元素。

相关问题