2017-10-12 57 views
0

我想取代實體爲以下XML,實體更換

<para>&#160;&#160;&#160;&#160;&#160;&#160;&#160;2015033555</para> 
<para>New York • Stuttgart • Delhi • Rio de Janeiro</para> 

輸出應該是

<para>&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;2015033555</para>   
<para>New York &#x2022; Stuttgart &#x2022; Delhi &#x2022; Rio de Janeiro</para> 

XSLT就像是,

<xsl:template match="//text()"> 

    <xsl:copy-of select="replace(.,'&#160;','&#x00A0;')"/> 
    <xsl:copy-of select="replace(.,'•','&#x2022;')"/>   
</xsl:template> 

使用上面提到的XSLT,它不是給予適當的輸出。你能幫助用來取代實體嗎?

回答

3

使用字符映射表(https://www.w3.org/TR/xslt-30/#character-maps):

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

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

    <xsl:character-map name="m1"> 
     <xsl:output-character character="&#160;" string="&amp;#x00A0;"/> 
     <xsl:output-character character="•" string="&amp;#x2022;"/>   
    </xsl:character-map> 

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

http://xsltransform.net/pNmBy22在線樣本。

請記住,XSLT處理器不知道輸入是否有一個字符字面或數字字符引用或爲十六進制字符引用或一些命名實體的參考,因爲它使用的底層XML解析器解析詞法將XML輸入到XSLT/XPath樹模型中,該模型只具有值爲Unicode字符序列的節點。因此,上面的字符映射方法將輸出XSLT輸出的任何非中斷空間,作爲序列&#x00A0;和任何點,如&#x2022;,與原始輸入標記無關。

+0

Thanku您的迴應馬丁。它工作正常。 – Sumathi