Brad Smith | 7 May 15:09

Querying valid children of an element?

Hello,

I just discovered lxml and am pretty excited about it. There is one
thing I'm having trouble figuring out how to do, though, if it's even
possible:

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? The best I've been able to come up with so far is to assemble a
tree of dummy nodes in the proposed order and then validate it, but
this seems wasteful.

Thanks in advance for any help offered,
--Brad

--

-- 
~ Second Shift: An original, serialized audio adventure ~
 http://www.secondshiftpodcast.com
Stefan Behnel | 8 May 09:33

Re: Querying valid children of an element?

Hi,

Brad Smith wrote:
> I just discovered lxml and am pretty excited about it.

:)

> 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.

> The best I've been able to come up with so far is to assemble a
> tree of dummy nodes in the proposed order and then validate it, but
> this seems wasteful.

Why? Don't you expect your users to get it right most of the time anyway? Why
don't you just assemble the complete result tree and validate that?

Is your program working on the tree itself or some other data representation?

This might be of interest:

(Continue reading)


Gmane