0

I'm trying to transform a document that has math elements with namespace xmlns="http://www.w3.org/1998/Math/MathML", transformation noT working if it has xmlns but it works if xmlns removed from the element I don't know what I'm missing here. I example with and without xmlns.

To refer code instantly check this link https://xsltfiddle.liberty-development.net/bET2rXa/1

Original Doc:

<?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>

Actual Transformation Output:

<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>
Pierre François
  • 5,850
  • 1
  • 17
  • 38
Jason
  • 65
  • 7
  • @Pierre François It looks similar to that question, but it doesn't answer it, I am aware that multiple xmlns should be declared in stylesheet root element, examples I added are from actual document. @ – Jason Jun 23 '21 at 10:58
  • The 1st `math` element in your XML is in a namespace - therefore your template does not match it and it is processed by the built-in template rules. This problem is the same as the one in the link. Note that in XSLT 2.0+ you can use `` instead of declaring a prefix. – michael.hor257k Jun 23 '21 at 11:41

0 Answers0