473,715 Members | 6,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Minidom empty script element bug

Hello All,

I ran into a problem while dynamically constructing XHTML documents using
minidom. If you create a script tag such as:

script_node_0 = self.doc.create Element("script ")
script_node_0.s etAttribute("ty pe", "text/javascript")
script_node_0.s etAttribute("sr c", "../test.js")

minidom renders it as:

<script src='../test.js' type='text/javascript'/>

Which is incorrect because:

XHTML 1.0 specs, Appendix C
~~~~@~~~~
C.3 Element Minimization and Empty Element Content

Given an empty instance of an element whose content model is not EMPTY (for
example, an empty title or paragraph) do not use the minimized form (e.g.
use <p> </p> and not <p />)
~~~~@~~~~

reference for further explanation:
http://lists.evolt.org/archive/Week-...04/105951.html

So, the rendered page completely fails on IE6 because it actually handles the
empty script element correctly. Mozilla handles the element incorrectly and
instantiates the javascript.

How do I get minidom to NOT render an empty script element? Should I submit a
bug report?

Thanks for the help,
Derek Basch


_______________ _______________ ____
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
Jul 18 '05 #1
4 2871
Derek Basch wrote:
XHTML 1.0 specs, Appendix C
~~~~@~~~~
C.3 Element Minimization and Empty Element Content

Given an empty instance of an element whose content model is not EMPTY (for
example, an empty title or paragraph) do not use the minimized form (e.g.
use <p> </p> and not <p />)
~~~~@~~~~
I'd like to point out that this is *not* a minidom bug. minidom cannot
possibly know that the document type is XHTML, and that strange, non-XML
rules apply to XHTML (i.e. rules which are not present in XML itself).

I'd also like to point out that XHTML Appendix C is informative (i.e.
non-normative), meaning that failure to comply to it does not imply
non-compliance with XHTML. An XML file which uses the minimized form
for the script element is still proper, well-formed, valid XHTML.
How do I get minidom to NOT render an empty script element? Should I submit a
bug report?


That said, I think there is a simple solution: add an empty Text node
to the script element:

script_node_0.a ppendChild(doc. createText(u"") )

[Disclaimer: this is untested; from reading the source, I think it
should work]

Regards,
Martin
Jul 18 '05 #2

Martin v. Löwis wrote:
Derek Basch wrote:
XHTML 1.0 specs, Appendix C
~~~~@~~~~
C.3 Element Minimization and Empty Element Content

Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use <p> </p> and not <p />)
~~~~@~~~~
I'd like to point out that this is *not* a minidom bug. minidom

cannot possibly know that the document type is XHTML, and that strange, non-XML rules apply to XHTML (i.e. rules which are not present in XML itself).
I'd also like to point out that XHTML Appendix C is informative (i.e.
non-normative), meaning that failure to comply to it does not imply
non-compliance with XHTML. An XML file which uses the minimized form
for the script element is still proper, well-formed, valid XHTML.
How do I get minidom to NOT render an empty script element? Should I submit a bug report?


That said, I think there is a simple solution: add an empty Text node
to the script element:

script_node_0.a ppendChild(doc. createText(u"") )

[Disclaimer: this is untested; from reading the source, I think it
should work]

Regards,
Martin

Thanks Martin. That fixed it. I had to change your code a bit to this:

script_node_0.a ppendChild(self .doc.createText Node(""))

maybe you meant createTextNode?

I started digging through the dom modules on this path:

XHTMLPrettyPrin t -> XHTMLPrinter -> Printer

and found this comment:

try:
#The following stanza courtesy Martin von Loewis
import codecs # Python 1.6+ only
from types import UnicodeType

So I guess you are pretty qualified to answer my question! You are
correct that this is not a minidom bug now that I think about it.

However, it seems proper that XHTMLPrinter (or some other module)
should allow the developer to use either normative or non-normative
XHTML design guidlines to achieve some sane degree of HTML user agent
compatablilty. Maybe something like this in Printer.py:

def visitElement(se lf, node):
...........
if len(node.childN odes):
self._write('>' )
self._depth = self._depth + 1
self.visitNodeL ist(node.childN odes)
self._depth = self._depth - 1
if not self._html or (node.tagName not in
HTML_FORBIDDEN_ END):
not (self._inText and inline) and self._tryIndent ()
self._write('</%s>' % node.tagName)
elif not self._html and node.tagName not in
XHTML_NON_NORMA TIVES:
self._write('/>')
elif node.tagName not in HTML_FORBIDDEN_ END:
self._write('></%s>' % node.tagName)
else:
self._write('>' )

of course this would only take care of the "C.3. Element Minimization
and Empty Element Content" guideline but you get the general idea.

Anyways, thanks for the help again and feel free to shoot down my
suggestions :)

Derek Basch

Jul 18 '05 #3
Derek Basch wrote:
maybe you meant createTextNode?
Yes, that's what I meant :-)
However, it seems proper that XHTMLPrinter (or some other module)
should allow the developer to use either normative or non-normative
XHTML design guidlines to achieve some sane degree of HTML user agent
compatablilty.
This is now PyXML, right? I also maintain PyXML...
Yes, XHtmlPrinter would be the right place to deal with XHTML
idiosyncrasies.
Anyways, thanks for the help again and feel free to shoot down my
suggestions :)


The general approach sounds good; feel free to submit a patch
to sf.net/projects/pyxml. I would recommend to implement Annex C
to the letter, i.e. only avoid the minimized form if the content
model is not EMPTY.

Regards,
Martin
Jul 18 '05 #4
Cross post from XML-SIG:

--- Walter Dörwald <wa****@livingl ogic.de> wrote:
Martin v. Löwis sagte:
Derek Basch wrote:
[...]
How do I get minidom to NOT render an empty script element? Should
I
submit a bug report?

That said, I think there is a simple solution: add an empty Text
node to the script element:

script_node_0.a ppendChild(doc. createText(u"") )

[Disclaimer: this is untested; from reading the source, I think it
should work]

If this doesn't work, you might want to try XIST
(http://www.livinglogic.de/Python/xist)
instead of minidom. XIST knows that the script element is not EMPTY, and when the
output is in HTML compatible XML an end tag will be produced:
from ll.xist.ns import html
print html.script(typ e="text/javascript", src="../test.js").asByt es(xhtml=1)
<script src="../test.js" type="text/javascript"></script>

Using pure XML mode gives:
print html.script(typ e="text/javascript",

src="../test.js").asByt es(xhtml=2)
<script src="../test.js" type="text/javascript"/>

Bye,
Walter Dörwald


Wow! XIST is very elegant. Perfectly designed for what it is supposed
to do.

"XIST is an extensible HTML/XML generator written in Python."

I guess there isn't much point in "fixing" the pyXML XHTMLPrinter when
something as cool as XIST exists (pun intended).

Kid also seems really neat. I like the TAL like features. However, it
seems less mature than XIST.

There seems to be lots of functionality crossover between the two but
it is good that there is enough demand for XML output functionality in
python to support two distinct modules.

Thanks Everyone!,
Derek Basch

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7510
by: xtian | last post by:
Hi - I'm doing some data conversion with minidom (turning a csv file into a specific xml format), and I've hit a couple of small problems. 1: The output format has a header with some xml that looks something like this: <item xmlns="" xmlns:thing="http://www.blah.com"> <thing:child name="smith"/> </item>
2
3253
by: Vincent De Baere | last post by:
Hi I am playing around a little with python (I've started using python at 1 PM today, so I guess I could be considered a newbie :-)), and have come across a little problem regarding xml.dom.minidom. this code snippet: menudoc = xml.dom.minidom.parse("menu.xml") menurootchildren = menudoc.documentElement.childNodes
0
1913
by: Scott F | last post by:
Hi all, Today's embarassingly simple question goes like this. I have a file, testdoc.xml, to parse. Inside the file is an element <refrain>Yo, Ho, Ho</refrain> So, starting with
4
6069
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
0
1908
by: Sullivan WxPyQtKinter | last post by:
Hi, I am now using minidom for my current development. I use cloneNode method in Element object, but it just does not work. The test code is very simple as follows: =========CODE============== from xml.dom.minidom import * a=Element('see') print a.toprettyxml() b=a.cloneNode(True)
1
1637
by: Dean Card | last post by:
I am using minidom to parse a 20,000 line XML file. I have a few instances where the number of child nodes of a particular node can be variable in number. To access them I am doing something like the following... xmldoc = minidom.parseString(r) results = xmldoc.childNodes for myNode in results.childNodes.childNodes: do Stuff with myNode...
1
1555
by: JoReiners | last post by:
Hello, I have a really strange problem. I'm unable to figure it out on my own. I parse very simple xml documents, without any check for their form. These files look very similar and are encoded in UTF-8. Now minidom is always able to parse these files with minidom.parse("file") . Now when fetching I use this expression: xmldoc.getElementsByTagName('DocNumb').firstChild.data.encode('latin1')
6
4481
by: Dan | last post by:
I'm using python's xml.dom.minidom module to generate xml files, and I'm running into memory problems. The xml files I'm trying to create are relatively flat, with one root node which may have millions of direct child nodes. Here's an example script: #!/usr/bin/env python import xml.dom.minidom
0
1516
by: Gary | last post by:
Howdy I ran into a difference between Python on Windows XP and Linux Fedora 6. Writing a dom to xml with minidom works on Linux. It gives an error on XP if there is an empty namespace. The problem was handled in CVS a while ago. http://mail.python.org/pipermail/xml-sig/2003-October/009904.html
0
8823
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9343
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.