簡體   English   中英

使用XSL在XML文檔中輸出HTML標簽

[英]Output HTML tags in XML document with XSL

關於我的問題,大約有一百萬個問題,我已經實現了大多數答案,但沒有任何運氣。

我正在編寫一個包含Web瀏覽器控件的C#應用​​程序。 在加載表單時,Webbrowser控件將加載以下XML。 使用下面的XSL為該XML設置樣式。

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並添加模板,但是再一次,沒有喜悅。

誰能幫忙。

提前致謝。

您控制輸入嗎? 您需要類似<P>這是一條消息。</ P>的格式(我將其標記為:

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

然后將主體的字符串值設為:

<P>This is a message.</P> 

使用disable-output-escaping可以在結果中構造一個P元素。 有了兩次轉義,您將無法期望&amp; lt; P&amp; gt; 創建一個元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM