2013-07-29 49 views
0

關於我的問題,大約有一百萬個問題,而且我沒有任何運氣實現了大多數答案。使用XSL在XML文檔中輸出HTML標籤

我正在寫一個C#應用程序,它包含一個webbrowser控件。在表單加載中,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並添加模板,但又一次沒有快樂。

任何人都可以幫忙。

在此先感謝。

+0

那麼你是否控制輸入?您需要格式爲' <P>這是一條消息。 </P >'(我將標記爲'這是一個消息。

]]>'),以便然後有所述主體的所述字符串值作爲'

這是一個消息。

'其中'disable-output-escaping'會在結果中構造一個'P'元素。隨着雙重轉義,你不能指望'& lt; p & gt;'創建一個元素。 –

+0

感謝馬丁,現在完美! –

回答

0

那麼你是否控制輸入?你需要一個格式,如<P>這是一條消息。 </P >(我將標記爲:

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

到然後有所述主體的所述字符串值作爲:

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

與禁用輸出-escaping將在結果中構造一個P元素。在雙重轉義的情況下,您不能指望& amp; lt; P & amp; gt;創建一個元素。