2012-02-24 60 views
3

我正在使用XSLT從文件中的某些節點中刪除不必要的屬性。不應轉換的節點使用以下簡單模板:如何在使用XSLT時保留轉義字符值?

<!-- Copy everything else over --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

但是,我發現某些節點丟失了其屬性值中包含的轉義字符。

例如,該輸入:

<field value="Lei Complementar No. 116/2003, Art. 6, &#167; 2&#186;, I."/> 

末這樣看:

<field value="Lei Complementar No. 116/2003, Art. 6, § 2º, I."/> 

如何避免這種不必要的轉型?

回答

3

I. XSLT 1.0溶液

就在這個屬性添加到xsl:output

encoding="us-ascii" 

這將導致其與字符代碼被呈現任何非ASCII字符。

但是,你仍然可以得到不同的輸出,如:

<field value="Lei Complementar No. 116/2003, Art. 6, &#167; 2&#186;, I."/> 

<field value="Lei Complementar No. 116/2003, Art. 6, &#xA7; 2&#xBA;, I." /> 

,當然,所有這三個都是相同的字符串的只是不同的represenatations( unicode)字符。

二, XSLT 2.0溶液

這使用<xsl:character-map>指令,並且必須總是產生相同的輸出:

<field value="Lei Complementar No. 116/2003, Art. 6, &#167; 2&#186;, I."/> 

:當所提供的XML文檔應用該變換

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output omit-xml-declaration="yes" 
    indent="yes" use-character-maps="chmEscapes"/> 

<xsl:character-map name="chmEscapes"> 
    <xsl:output-character character="&#167;" 
         string="&amp;#167"/> 
    <xsl:output-character character="&#186;" 
         string="&amp;#186"/> 
</xsl:character-map> 

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

想要的,正確的結果產生

<field value="Lei Complementar No. 116/2003, Art. 6, &#167 2&#186, I."/> 
相關問題