[lxml-dev] Querying valid children of an element?
Stefan Behnel
stefan_ml at behnel.de
Thu May 8 09:33:17 CEST 2008
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:
http://codespeak.net/lxml/element_classes.html
Stefan
More information about the lxml-dev
mailing list