微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XSLT:如果元素中包含“xmlns”,则不会转换 xml 元素,

如何解决XSLT:如果元素中包含“xmlns”,则不会转换 xml 元素,

我正在尝试转换具有 math 元素且命名空间为 xmlns="http://www.w3.org/1998/Math/MathML" 的文档,如果它具有 xmlns,则转换不起作用,但如果 xmlns 从元素我不知道我在这里缺少什么。 我以使用和不使用 xmlns 为例。

要立即引用代码,请查看此链接 https://xsltfiddle.liberty-development.net/bET2rXa/1

原始文档:

<?xml version="1.0" encoding="utf-8" ?>
<html>
    <head>Test Document</head>
    <body>
        <article attr1="1" attr2="2">
        <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mrow><mi>a</mi></mrow><mrow><mn>2</mn></mrow></msup><mo>+</mo><msup><mrow><mi>b</mi></mrow><mrow><mn>2</mn></mrow></msup><mo> = </mo><msup><mrow><mi>c</mi></mrow><mrow><mn>2</mn></mrow></msup></math>
        <math><msup><mrow><mi>a</mi></mrow><mrow><mn>2</mn></mrow></msup><mo>+</mo><msup><mrow><mi>b</mi></mrow><mrow><mn>2</mn></mrow></msup><mo> = </mo><msup><mrow><mi>c</mi></mrow><mrow><mn>2</mn></mrow></msup></math>
        </article>
    </body>
</html>

实际转换输出

<html>
    <head><Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">Test Document</head>
    <body>
        <article attr1="1" attr2="2">
        a2+b2 = c2
        <math><msup><mrow><mi>a</mi></mrow><mrow><mn>2</mn></mrow></msup><mo>+</mo><msup><mrow><mi>b</mi></mrow><mrow><mn>2</mn></mrow></msup><mo> = </mo><msup><mrow><mi>c</mi></mrow><mrow><mn>2</mn></mrow></msup></math>
        </article>
    </body>
</html>

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" 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>
  
  <xsl:template match="math | math//* | math//@*">
      <xsl:apply-templates select="." mode="copy-after-all"/>
  </xsl:template>  
</xsl:stylesheet>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。