2017-10-12 62 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