You need to enable JavaScript to run this app. XSLT无法转换携带xmlns命名空间的math元素该如何解决
优惠活动
大模型
产品
解决方案
定价
更多

XSLT无法转换携带xmlns命名空间的math元素该如何解决

问题根因
  • XPath的元素匹配是严格区分命名空间的,你当前的XSLT中没有声明MathML的命名空间,匹配规则match="math | math//* | math//@*"只能匹配没有命名空间的math元素,无法命中带有http://www.w3.org/1998/Math/MathML命名空间的math元素。
  • 未命中特殊匹配模板的带命名空间math元素会命中默认的match="*"模板,该模板只会递归输出子节点的文本内容,不会保留元素结构,这就是带命名空间的math被转换为纯文本的原因。
解决方法

在XSLT中声明MathML命名空间,同时修改匹配规则覆盖带命名空间和不带命名空间的两种math元素场景,修改后的XSLT代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:m="http://www.w3.org/1998/Math/MathML"
                version="3.0">
  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="html" indent="no" html-version="5"/>

  <xsl:template match="*">
     <xsl:apply-templates />
  </xsl:template>

    <xsl:template match="@*">
         <xsl:apply-templates />
    </xsl:template>
  
  <xsl:template match="node() | @*" mode="copy-after-all">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="html | head | body | article | article/@*">
      <xsl:apply-templates select="." mode="copy-after-all"/>
  </xsl:template>
  
  <!-- 同时匹配带命名空间和不带命名空间的math及其子节点、属性 -->
  <xsl:template match="math | math//* | math//@* | m:math | m:math//* | m:math//@*">
      <xsl:apply-templates select="." mode="copy-after-all"/>
  </xsl:template>  
</xsl:stylesheet>

修改后两种math元素都会保留原始结构输出。

内容的提问来源于stack exchange,提问作者Jason

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 20:54:06