473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Python & XML & DTD (warning: noob attack!)

Hello all,

I have an XML file with an internal DTD which looks roughly like this:

<?xml version="1.0"?>
<!DOCTYPE root [
<!ELEMENT root (node)*>
<!ELEMENT node (description, info, node*)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT info EMPTY>
<!ATTLIST info
text CDATA #REQUIRED

]>
<root>
<node>
<description>node 1</description>
<info text="info 1" />
<node>
<description>node 1-1</description>
<info text="info 1-1" />
</node>
</node>
<node>
<description>node 2</description>
<info text="info 2" />
<node>
<description>node 2-1</description>
<info text="info 2-1" />
</node>
<node>
<description>node 2-2</description>
<info text="info 2-2" />
</node>
</node>
</root>

I want to parse this file into my application, modify the data (this includes
maybe creating and/or deleting nodes), and write it back into the file --
including the DTD. (It doesn't necessarily need validation, though.)

I tried xml.dom.ext.PrettyPrint, but it produces only

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE root>
<root>
...
</root>

actually lacking the document type definition.

Any help is appreciated!

Thanks in advance & cheers =)
*igor*
Jul 18 '05 #1
4 1848
Igor Fedorow wrote:

I have an XML file with an internal DTD which looks roughly like this:
[snip]
I want to parse this file into my application, modify the data (this includes
maybe creating and/or deleting nodes), and write it back into the file --
including the DTD. (It doesn't necessarily need validation, though.)

I tried xml.dom.ext.PrettyPrint, but it produces only
[snip]
actually lacking the document type definition.

Any help is appreciated!


Unfortunately I don't know of any way you could generate the DTD again,
and I've never seen a package which supports what you ask for (not that
it isn't possible, mind you).

On the other hand, are you sure you need the DTD? We use XML in
dozens of ways and absolutely have never benefited from attempts
to use DTDs, and don't appear to suffer from the lack thereof.

Also, aren't DTDs sort of considered either obsolete or at least
vastly inferior to the newer approaches such as XML Schema, or both?

So my recommendation is to ditch the DTD and see if any problems
arise as a result.

-Peter
Jul 18 '05 #2
On Thu, 29 Jan 2004 15:44:25 +0100, Peter Hansen wrote:
Igor Fedorow wrote:

I have an XML file with an internal DTD which looks roughly like this:
[snip]
I want to parse this file into my application, modify the data (this
includes maybe creating and/or deleting nodes), and write it back into the
file -- including the DTD. (It doesn't necessarily need validation,
though.)

I tried xml.dom.ext.PrettyPrint, but it produces only [snip] actually
lacking the document type definition.

Any help is appreciated!


Unfortunately I don't know of any way you could generate the DTD again, and
I've never seen a package which supports what you ask for (not that it isn't
possible, mind you).

On the other hand, are you sure you need the DTD? We use XML in dozens of
ways and absolutely have never benefited from attempts to use DTDs, and
don't appear to suffer from the lack thereof.

Also, aren't DTDs sort of considered either obsolete or at least vastly
inferior to the newer approaches such as XML Schema, or both?

So my recommendation is to ditch the DTD and see if any problems arise as a
result.

-Peter


Actually, I don't really *need* it, but I would simply like to have it -- which
obviously isn't possible...

Anyway, thank you for your help!

Cheers =)
*igor*
Jul 18 '05 #3
On 2004-01-29 07:04:44 -0500, Igor Fedorow <ig*********@obda.net> said:
I tried xml.dom.ext.PrettyPrint, but it produces only

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE root>
<root>
...
</root>

actually lacking the document type definition.


Why not simply use that, then replace the <!DOCTYPE root> with the DTD? I'm
sure you can parse it out from the original file.

--
"Remember, no matter where you go, there you are."
-Buckaroo Banzai
Kevin Ballard
http://kevin.sb.org
Jul 18 '05 #4
Peter Hansen <pe***@engcorp.com> wrote:
Unfortunately I don't know of any way you could generate the DTD again
It is possible to preserve the internal subset in DOM Level 3. You can
read it from the property DocumentType.internalSubset, and it will be
included in documents serialised by an LSSerializer.

It is not, however, possible to write to the internalSubset, and you can't
create a new DocumentType object with a non-empty internalSubset, for some
reason. So the only standard way to copy an internalSubset would be to make
the new document by parsing something with the same value, eg.:

dtd= oldDocument.doctype.internalSubset
parser= oldDocument.implementation.createLSParser(1, None)
input= oldDocument.implementation.createLSInput()
input.stringData= '<!DOCTYPE x [%s]><x/>' % dtd
newDocument= parser.parse(input)
I've never seen a package which supports what you ask for
Plug time: the only package I know of to support DOM Level 3 is my own:

http://www.doxdesk.com/software/py/pxdom.html

Currently this is based on the November 2003 CR spec; there have been a
number of fixes and changes to L3 functionality since, but I'm waiting for
W3C to publish the next draft (presumably Proposed Recommendation) before
releasing 1.0.
Also, aren't DTDs sort of considered either obsolete or at least
vastly inferior to the newer approaches such as XML Schema, or both?


Certainly they have their drawbacks: they're namespace-ignorant, not
flexible enough for some purposes, and they're a legacy bag on the side of
XML rather than something built on top of it in XML syntax.

Still, they're well-understood and widely supported, and simpler to learn
than Schema at least.

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #5

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

Similar topics

0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
0
by: James Thurley | last post by:
I'm creating an XmlDocument manually, adding content using the Xml classes such as XmlElement and XmlText, and I then write it out as as "text/xml" to the HttpResponse.Output TextWriter object...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
24
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread...
3
by: Roger F | last post by:
Help Please When I insert code into my database ie. This & That I see it is the same in the data base but when I go to view it, it as changed into. This &amp; That What is happening, rather a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.