3

我使用 XSL 样式表(使用 Apache Xalan)将 XML 转换为(某种程度的)HTML。在 XML 中可以有像 一样的实体—,它们必须保持原样。在 XML 文件的开头,我有一个引用这些实体的文档类型。我应该怎么做才能使实体保持不变?

<!DOCTYPE article [
<!ENTITY mdash "&mdash;"><!-- em dash -->
]>

在 XML 文本中SAXParseException: Recursive entity expansion, 'mdash'遇到时给我。&mdash

4

2 回答 2

5

The way to define and use the entity is:

<!DOCTYPE xsl:stylesheet [<!ENTITY mdash "&#x2014;">]>
<t>Hello &mdash; World!</t>

When processed with the simplest possible XSLT stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
</xsl:stylesheet>

The correct output (containing mdash) is produced:

Hello — World!

Important:

In XSLT 2.0 it is possible to use the <xsl:character-map> instruction so that certain, specified characters are represented by entities. In this particular case:

<xsl:stylesheet   version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output omit-xml-declaration="yes" 
    use-character-maps="mdash"/>
  <xsl:character-map name="mdash">
    <xsl:output-character character="&#x2014;" string="&amp;mdash;" />
  </xsl:character-map>

</xsl:stylesheet>

when the above transformation is applied on the same XML document (already shown above), the output is:

Hello &mdash; World!
于 2010-04-28T13:22:43.590 回答
1
于 2010-04-28T13:21:36.833 回答