0

In python i want to get a list of required attributes from an element as defined in a dtd file? Is there any posibility to do this?

<!ELEMENT Book EMPTY>
<!ATTLIST Book
isbn CDATA #REQUIRED
title CDATA #REQUIRED
author CDATA #REQUIRED
release CDATA #IMPLIED
>

I want to get all required attributes to this element. "isbn", "title", and "author".

My python arrays should look like this:

bookRequiredAtts = {"isbn","title","author"}
bookOptionalAtts = {"release"}

1 Answer 1

-1

Parse it with beautifulsoup. You can then find the attributes you want.

Here is some code to help you:

from BeautifulSoup import BeautifulStoneSoup
xml = "<!ELEMENT Book EMPTY>
<!ATTLIST Book
isbn CDATA #REQUIRED
title CDATA #REQUIRED
author CDATA #REQUIRED
release CDATA #IMPLIED
>
"
soup = BeautifulStoneSoup(xml)
soup.findAll(isbn=True,title=True)

Refer to the documentation for any other help.

5
  • I'm going to try this later. Thank you
    – Thomas
    Mar 8, 2012 at 10:49
  • I didn't find anything that worked for me. Are you sure about beautifulsoup. If so, please give me an example. I added a dtd example to my question.
    – Thomas
    Mar 8, 2012 at 12:55
  • I don't think BeautifulSoup can parse DTD.
    – wRAR
    Mar 8, 2012 at 12:55
  • It says clearly here that you can use 'BeautifulStoneSoup' to parse DTD. Not all DTD, but it can be done.
    – user850498
    Mar 8, 2012 at 13:24
  • I'm not trying to parse dtd. I want to get information about restrictions and names from a dtd file!
    – Thomas
    Mar 8, 2012 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.