Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:45:55+00:00 2026-06-15T02:45:55+00:00

When rendering XHTML with lxml, everything is fine, unless you happen to use Firefox,

  • 0

When rendering XHTML with lxml, everything is fine, unless you happen to use Firefox, which seems unable to deal with namespace-prefixed XHTML elements and javascript. While Opera is able to execute the javascript (this applies to both jQuery and MathJax) fine, no matter whether the XHTML namespace has a prefix (h: in my case) or not, in Firefox the scripts will abort with weird errors (this.head is undefined in the case of MathJax).

I know about the register_namespace function, but it does neither accept None nor "" as namespace prefix. I’ve heard about _namespace_map in the lxml.etree module, but my Python complains that this attribute doesn’t exist (version issues?)

Is there any other way removing the namespace prefix for the XHTML namespace? Note that str.replace, as suggested in the answer to another, related question, is not a method I could accept, as it is not aware of XML semantics and might easily screw up the resulting document.

As per request, you’ll find two examples ready to use. One with namespace prefixes and one without. The first one will display 0 in Firefox (wrong) and the second one will display 1 (correct). Opera will render both correct. This is obviously a Firefox bug, but this only serves as a rationale for wanting prefixless XHTML with lxml – there are other good reasons as to reduce traffic for mobile clients etc (even h: is quite a lot if you consider tens or hundret of html tags).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:45:56+00:00Added an answer on June 15, 2026 at 2:45 am

    This XSL transformation removes all prefixes from content, while maintaining namespaces defined in the root node:

    import lxml.etree as ET
    
    content = '''\
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE html>
    <h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ml="http://foo">
      <h:head>
        <h:title>MathJax Test Page</h:title>
        <h:script type="text/javascript"><![CDATA[
          function test() {
            alert(document.getElementsByTagName("p").length);
          };
        ]]></h:script>
      </h:head>
      <h:body onload="test();">
        <h:p>test</h:p>
        <ml:foo></ml:foo>
      </h:body>
    </h:html>
    '''
    dom = ET.fromstring(content)
    
    xslt = '''\
    <xsl:stylesheet version="1.0"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="no"/>
    
    <!-- identity transform for everything else -->
    <xsl:template match="/|comment()|processing-instruction()|*|@*">
        <xsl:copy>
          <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>
    
    <!-- remove NS from XHTML elements -->
    <xsl:template match="*[namespace-uri() = 'http://www.w3.org/1999/xhtml']">
        <xsl:element name="{local-name()}">
          <xsl:apply-templates select="@*|node()" />
        </xsl:element>
    </xsl:template>
    
    <!-- remove NS from XHTML attributes -->
    <xsl:template match="@*[namespace-uri() = 'http://www.w3.org/1999/xhtml']">
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>
    </xsl:stylesheet>
    '''
    
    xslt_doc = ET.fromstring(xslt)
    transform = ET.XSLT(xslt_doc)
    dom = transform(dom)
    
    print(ET.tostring(dom, pretty_print = True, 
                      encoding = 'utf-8'))
    

    yields

    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>MathJax Test Page</title>
        <script type="text/javascript">
          function test() {
            alert(document.getElementsByTagName("p").length);
          };
        </script>
      </head>
      <body onload="test();">
        <p>test</p>
        <ml:foo xmlns:ml="http://foo"/>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rendering the same html in IE and Firefox gives me a different result because
What underlying rendering system does the Qt GUI toolkit use on Windows? (I'm assuming
I have some weird problem in rendering some xhtml in IE 8 http://img709.imageshack.us/i/scrj.png/ .
Seems Safari is having some problems rendering my SVG, while other browsers does it
I'm having trouble rendering an ASP.NET gridview with internet explorer 8. It works fine
Are there any noted differences in appearance rendering of HTML and XHTML in Google
I peeked inside the source of a public website which uses the XHTML 1.0
i work for a company where all our sites currently use the XHTML 1.0
Currently the page I am working displays fine in Chrome and Firefox, but in
I've just installed grails rendering plugin and would like to use it for generating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.