2016-09-14 67 views
0

在一個XML文件,我有一些標籤是這樣的:XSLT 1.0:如何避免ascii轉換?

<xsl:value-of select="foo"/> 

我得到的輸出:this is a "test"

<foo>this is a &quot;test&quot;</foo> 

當我處理它。

我想回去是文本(因爲它)

this is a &quot;test&quot; 

沒有任何類型的轉換/處理。

我該如何問XSLT 1.0以避免任何「處理」?

我想:

<xsl:value-of disable-output-escaping="yes|no" /> 

,但它不工作。

如果有解決方案,可以將它作爲XSLT文件中所有<xsl:value-of />的「默認」?

+0

你生產什麼類型的輸出? ''? –

+0

一樣,沒有什麼變化。 – Randomize

+0

這不完全是我的問題的答案... –

回答

0

如果你有XML <foo>this is a &quot;test&quot;</foo>那麼任何XML解析器任何XSLT處理器採用解析實體引用&quot;成Unicode字符的"和XSLT處理器將與foo元素節點工作,具有與該字符串值this is a "test"文本子節點,這意味着XSLT處理器將不知道您的原始XML是否具有Unicode字符"或實體參考&quot;

因此,沒有辦法保留實體引用(除非預處理XML並將實體引用轉換爲XSLT可以區分的標記,請參閱http://andrewjwelch.com/lexev/以獲取Java世界中的選項)。

使用XSLT 2.0或3.0,您可以使用字符映射https://www.w3.org/TR/xslt20/#character-maps將結果中的任何引號字符映射到實體引用。然而,這不會保留輸入中的實體引用,而是輸出任何引號字符作爲字符引用。見http://xsltransform.net/bwdwrK這確實

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

    <xsl:output use-character-maps="escape"/> 

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

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:transform> 

和轉換

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <foo>This is a &quot;test&quot;.</foo> 
    <foo>This is another "test".</foo> 
</root> 

<?xml version="1.0" encoding="UTF-8"?><root> 
    <foo>This is a &quot;test&quot;.</foo> 
    <foo>This is another &quot;test&quot;.</foo> 
</root> 
+0

不幸的是,我只能使用XSLT 1.0 – Randomize

+0

或者我可以用單引號替換"'? – Randomize