8 May 22:27
Re: Querying valid children of an element?
From: Brad Smith <usernamenumber <at> gmail.com>
Subject: Re: Querying valid children of an element?
Newsgroups: gmane.comp.python.lxml.devel
Date: 2008-05-08 20:27:02 GMT
Subject: Re: Querying valid children of an element?
Newsgroups: gmane.comp.python.lxml.devel
Date: 2008-05-08 20:27:02 GMT
To clarify about what I'm doing. The goal is to have a shorthand
language (not entirely tag-based) that is easier for subject matter
experts to learn than docbook, which can then be converted into full
docbook once they've written a first draft. So, to illustrate one
aspect of it, instead of writing
<itemizedlist>
<listitem><para><command>foomaster</command> example...</para>
<itemizedlist>
<listitem><screen>$ foomaster [OPTIONS]</screen></listitem>
</itemizedlist>
</listitem>
</itemizedlist>
They can write
* <command>foomaster</command> example...
** <screen>$ foomaster [OPTIONS]</screen>
As you can see, the translation process consists of not just
converting asterisks into the appropriate combination of itemizedlists
and listitems, but also protecting cdata within paras where necessary.
In the first one, the interpreter sees that <command> isn't allowed
inside <listitem>, which is its cue to try inserting a <para>.
<screen> is allowed within <listitem>, so it does not insert a para.
Making that determination is what I'm trying to find the best approach
for.
Currently I use a function like this:
def validateAppend(parent,child):
parent.append(child)
if not dtd.validate(parent):
dbg("Appending %s to %s failed DTD validation" % (child.tag,parent.tag))
del(parent[-1])
return False
return True
This works but, like I said, is not terribly efficient, so I just
wanted to see if there was another method for making the
determination.
--Brad
On Thu, May 8, 2008 at 11:44 AM, Mike Meyer <mwm <at> mired.org> wrote:
> On Thu, 08 May 2008 09:33:17 +0200 Stefan Behnel <stefan_ml <at> behnel.de> wrote:
>
>> > I am writing a tool that translates xml tags mixed with a wiki-like
>> > shorthand into full xml. It would be helpful to be able to
>> > sanity-check the mix of explicit tags and implicit tags I'm deriving
>> > from the shorthand by querying our DTD along the lines: "Is element
>> > foo legal within element bar" Same for CDATA.
>> >
>> > Is this possible using lxml? If not, is it possible using anything
>> > else?
>>
>> You could define your grammar in a way that is easily usable for you in your
>> program and then generate a DTD from that.
>
> Are you really using DTDs, and not using that as a catchall for the
> various Schema languages?
>
> If so, then you might consider switching to a modern schema
> language. RelaxNG lets you write regular expressions for CDATA, which
> ought to work with wiki-like "tags", and I wouldn't be surprised to
> find that Schematron is turing complete.
>
> <mike
>
--
--
~ Second Shift: An original, serialized audio adventure ~
http://www.secondshiftpodcast.com
RSS Feed