2015-04-07 89 views
0

我正在進行XSL 1.0轉換,以在Firefox中顯示XML時獲取HTML可視化。 我在原來的XML,我有這樣XSL中的特殊字符

é è ‘... 

字符我需要將它們轉換成

é, è, ‘... 

我已經使用這個模板:

<xsl:template name="string-replace-all"> 
    <xsl:param name="text" /> 
    <xsl:param name="replace" /> 
    <xsl:param name="by" /> 
    <xsl:choose> 
    <xsl:when test="contains($text, $replace)"> 
     <xsl:value-of select="substring-before($text,$replace)" /> 
     <xsl:value-of select="$by" /> 
     <xsl:call-template name="string-replace-all"> 
     <xsl:with-param name="text" select="substring-after($text,$replace)" /> 
     <xsl:with-param name="replace" select="$replace" /> 
     <xsl:with-param name="by" select="$by" /> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="$text" /> 
    </xsl:otherwise> 
    </xsl:choose> 

呼喚每個特殊字符(這裏例如& egrave;):

  <xsl:variable name="newtext"> 
       <xsl:call-template name="string-replace-all"> 
       <xsl:with-param name="text" select="$originaltext" /> 
       <xsl:with-param name="replace" select="'&amp;egrave;'" /> 
       <xsl:with-param name="by" select="'è'" /> 
       </xsl:call-template> 
      </xsl:variable> 

是否有解決方案,我可以直接將&amp;替換爲&,例如,無需爲每個我希望存在的特殊字符調用替換模板?

+1

我能想到的唯一改進就是不用切換到XSLT 2。0,將在您的樣式表中創建特殊字符的「表」,並循環遍歷它,爲前一次調用的輸出中的每個「行」調用'string-replace-all'。不過,涉及擴展功能的選項可能更好,例如http://exslt.org/dyn/functions/evaluate/index.html,具體取決於您使用的XSLT處理器。 – LarsH

+0

我需要使用Firefox直接顯示帶有其轉換的XML,而無需特殊的預處理器。 –

+1

好吧,既然Firefox是你的環境,爲什麼不讓你的樣式表輸出一些Javascript。在加載頁面後,JS代碼可以通過'&'替代'&'。我不確定它會起作用,但值得一試。 – LarsH

回答

1

這似乎爲我(老版本的)Firefox的工作:

XML

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="mystyle.xsl"?> 
<root> 
    <description>Article Containing Escaped Entitites</description> 
    <content>Lor&amp;eacute;m ipsum &amp;lsquo;dolor&amp;lsquo; sit am&amp;egrave;t, consectetuer adipiscing elit.</content> 
</root> 

mystyle.xsl中

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/root"> 
    <html> 
     <body> 
      <h2><xsl:value-of select="description"/></h2> 
      <p id="content"> 
       <xsl:value-of select="content"/> 
      </p> 

      <script> 
    var element = document.getElementById("content"); 
    element.innerHTML = element.innerHTML.replace(/&amp;amp;/g,'&amp;'); 
      </script> 

     </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

結果(截圖):

enter image description here

警告:我不是Javascript專家;這只是我衝動拼湊在一起的東西。

+0

這是一個有用的答案。 – LarsH

+0

我相信這是對我而言最好的方法,謝謝 –

-2
String htmlstring = "Put Your HTML string here" 
      + htmlstringbuf 
        .toString() 
        .replaceAll("&nbsp;", " ") 
        .replaceAll("&", "&amp;") 
        .replaceAll("null", " ") 
        .replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>"," ") 
        .replaceAll("Â", "<br></br>") 
        .replaceAll("<\\?xml version = '1.0' encoding ='UTF-8'\\?>", 
          " ") + "</body>"; 
+0

謝謝你的回答。你能告訴我在哪裏插入這個腳本?我有麻煩測試出來 –

+0

這是創建html字符串的代碼,並從字符串替換所有特殊字符把這個到你的正在製作html字符串的位置....可能這會工作,並幫助你... –

+0

這是打算JavaScript的? java嗎?你如何建議將它集成到XSLT樣式表中?另外,大部分對'replaceAll()'的調用都沒有做OP所要求的。 – LarsH

1

有沒有一個解決方案,我可以直接替換成& &爲 例如無需調用每個 特殊字符我希望存在的更換模板?

爲什麼不簡單地在輸出文本時禁用轉義?例如,給定的輸入:你可以有你的樣式表過程

<content>Lor&amp;eacute;m ipsum &amp;lsquo;dolor&amp;lsquo; sit am&amp;egrave;t, consectetuer adipiscing elit.</content> 

此爲:

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

,並返回:

<p>Lor&eacute;m ipsum &lsquo;dolor&lsquo; sit am&egrave;t, consectetuer adipiscing elit.</p> 

該瀏覽器應該呈現:

enter image description here

+0

感謝您的迴應,問題是我有雙重編碼,例如「à」編碼爲「& agrave;」在我的XML中。當我使用disable-output-escaping時,瀏覽器中的最終結果是「&agrave;」 –

+0

@SouhaibGuitouni爲什麼這是一個問題?這正是它需要的:在XML中轉義(「& agrave;」),在HTML中未轉義(「&agrave;」)。 –

+1

我一直在尋找問題的原因,瀏覽器(在我的情況下Firefox)不處理禁用輸出轉義。它只是忽略它。 http://stackoverflow.com/questions/1137241/why-is-the-xslt-disable-output-escaping-not-implemented-in-firefox 因此,& agrave;總是顯示出來; –