2016-08-01 77 views
0

我在XML下面我試圖在一些輸出文件將這一說HTML轉換編碼,XSLT

是否有可能轉換到&在&輸出文件?

<Property> 
    <name>Document Title</name> 
    <value>Me &amp; you</value> 
</Property> 
+0

你能解釋一下這個問題到底是什麼嗎?在HTML中,與XML一樣,&字符通常編碼爲'&',所以在HTML輸出中的何處/爲什麼需要非轉義的&符號? –

回答

1

正如你所標記的問題作爲XSLT 2.0,如果你真的想在HTML輸出轉義符號&那麼你可以使用字符映射表:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

    <xsl:output method="html" use-character-maps="m1"/> 

    <xsl:character-map name="m1"> 
     <xsl:output-character character="&amp;" string="&amp;"/> 
    </xsl:character-map> 

    <xsl:template match="/"> 
     <html> 
      <body> 
       <p>This is a test of the encoding of the <code>&amp;</code> character.</p> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

輸出是

<html> 
    <body> 
     <p>This is a test of the encoding of the <code>&</code> character. 
     </p> 
    </body> 
</html> 

但是,在XML中,與&符號通常會被轉義爲&amp;,因爲某種原因,如果您的XSLT處理器在HTML輸出中轉義它,那麼它通常會執行nting HTML語法規則。