2014-04-07 34 views
1

我想達到以下XML輸出:XSLT:輸出"而無需解析

<Foo bar="&quot;" /> 

我的XSLT文件如下:

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

     <xsl:variable name="quote"> 
      <xsl:text>&quot;</xsl:text> 
     </xsl:variable> 

     <Foo bar="{$quote}"/> 
    </xsl:template> 
</xsl:stylesheet> 

不幸的是,這給了我的輸出:

<Foo bar="&#34;"/> 

我如何改變我的XSLT輸出& QUOT;沒有它被解析成「字符或&#34 ;?

+4

爲什麼它很重要的兩者是等價的(也有其他各種等價的方式說同樣的事情,例如''),並且XML解析器不會告訴應用程序以哪種方式寫入源代碼,應用程序只會看到其值爲雙引號字符的屬性。 –

+1

你說得很好。我一直致力於以特定格式輸出XML - 以匹配Microsoft Excel似乎需要的內容,但即使使用",但您仍然正確地解析文件。 謝謝! –

回答

0

伊恩羅伯茨已經提出了很好的觀點,它實際上並不重要,但如果你真的,真的想這樣做,然後在XSLT 2.0(而不是XSLT 1.0),你可以使用字符映射表的,就像這樣:?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" use-character-maps="quotes" /> 

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

    <xsl:template match="/"> 
     <xsl:variable name="quote"> 
      <xsl:text>&quot;</xsl:text> 
     </xsl:variable> 

     <Foo bar="{$quote}"/> 
    </xsl:template> 
</xsl:stylesheet>