1

我需要 使用 XSLT 1.0 将实体转换为普通空间(键盘空间)。但我得到的是实体 而不是空间。

示例 XML:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

XSLT 1.0 试过:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="mtext">
<xsl:for-each select="contains(.,'&#x00A0;')">
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

所需输出:

<?xml version='1.0' encoding='UTF-8' ?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML"><math display="block"><mrow><mtext>x y + y x</mtext></mrow></math></chapter>

输出:

<?xml version='1.0' encoding='UTF-8' ?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML"><math display="block"><mrow><mtext>x&#160;y&#160;+&#160;y&#160;x</mtext></mrow></math></chapter>
4

2 回答 2

4

简单得多

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
</xsl:template>

这是一个完整的转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="text()">
     <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

产生了想要的正确结果:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
   <math display="block">
      <mrow>
         <mtext>x y + y x</mtext>
      </mrow>
   </math>
</chapter>

记住

用另一个字符替换单个字符(或删除它)的最佳方法是使用标准 XPath 函数translate()

于 2013-01-25T14:06:38.170 回答
1
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
  <xsl:output method="xml" encoding="UTF-8" indent="no"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="mtext">
    <xsl:call-template name="replace-string">
      <xsl:with-param name="text" select="."/>
      <xsl:with-param name="from">&#x00A0;</xsl:with-param>
      <xsl:with-param name="to" select="' '"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="from"/>
    <xsl:param name="to"/>
    <xsl:choose>
      <xsl:when test="contains($text, $from)">
        <xsl:variable name="before" select="substring-before($text, $from)"/>
        <xsl:variable name="after" select="substring-after($text, $from)"/>
        <xsl:variable name="prefix" select="concat($before, $to)"/>
        <xsl:copy-of select="$before"/>
          <xsl:value-of select="$to" disable-output-escaping="yes"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="$after"/>
          <xsl:with-param name="from" select="$from"/>
          <xsl:with-param name="to" select="$to"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
于 2013-01-25T10:04:46.410 回答