xsl-list
[Top] [All Lists]

Re: [xsl] HTML5 and MathML and namespaces, oh my

2020-11-05 19:12:43
Hi Wendell, all,

My “xpath-default-namespace" solution worked when I modified only MathML 
content, but it failed when I tried to modify both MathML and HTML content.  :(

Your note that XSLT documents themselves follow XML namespace rules got me 
thinking… and I also came across the November 2013 solution from Ian Roberts 
here, suggesting pertinent default namespaces on the <xsl:template> element 
themselves:

https://stackoverflow.com/questions/20090833/best-solution-dealing-with-default-namespaces-in-xml-and-xslt

and that seemed like a perfect and intuitive solution! But I can’t get it to 
work.

Given this input document (HTML top level, MathML within):


<html>
  <head>
    <title>Equations</title>
  </head>
  <body>
    <p>
      <math xmlns="http://www.w3.org/1998/Math/MathML";>
        <mi>m</mi>
      </math>
    </p>
  </body>
</html>


and this stylesheet:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="xs"
  version="2.0">

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

  <xsl:template match="p">
    <div>
      <xsl:copy-of select="."/>
    </div>
  </xsl:template>

  <xsl:template xmlns="http://www.w3.org/1998/Math/MathML"; match="mi">
    <mrow>
      <xsl:copy-of select="."/>
    </mrow>
  </xsl:template>

</xsl:stylesheet>


the template for HTML <p> fires, but the template for MathML <mi> does not. 
What am I doing wrong? I tried other solutions and I haven’t found anything to 
allow me to modify content in both namespace domains.

Thanks!!


  *   Chris


From: Wendell Piez 
wapiez(_at_)wendellpiez(_dot_)com<mailto:wapiez(_at_)wendellpiez(_dot_)com> 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>>
Sent: Wednesday, November 4, 2020 11:12 AM
To: 
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com<mailto:xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: Re: [xsl] HTML5 and MathML and namespaces, oh my

Chris,

Awesome. You have about cracked the namespace conundrum. Part of the key is to 
see that the XSLT is also an XML document and follows all the same rules as any 
other XML document as to its namespaces.

So:

Why would the XSLT serializer write explicit namespace references that aren’t 
needed by context?

In this case, all the declarations in samples you have shown are needed with 
the exception of the stray binding to an 'mml' prefix, which you removed. They 
would not be needed if the processor (say) assigned all its own prefixes to 
names, instead of using the names as given in the XSLT. But we would hate that. 
So it does its best to give us what we need, with the names it thinks we want.

Isn’t the point of namespaces that everything is fully qualified internally, 
then the output can adjust accordingly?

Yes! As you've discovered, that can require some jiggery when multiple 
vocabularies are competing for unprefixed names (or the same prefixes).

How do I get a clean <math> island of MathML content without namespace stuff 
cluttering up its contents?

Bravo! Actually with a little practice you can make it so your code is 
reasonably clean even in mixed-namespace use cases.

Old project: 
https://github.com/wendellpiez/XMLNamespaceFixup<https://urldefense.com/v3/__https:/github.com/wendellpiez/XMLNamespaceFixup__;!!A4F2R9G_pg!Jhl_IDRYqlo2VzhBevEPT2OWcoGcLIPmsruLOTbumPfCYoJ_cCkeVydWWU_RjqAl8DdrvKRO1IBfYvM$>
 (but if you do things right you'll never need it).

Cheers, Wendell

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>