[lxml-dev] Resolve RelaxNG document

Stefan Behnel stefan_ml at behnel.de
Thu Sep 6 15:59:30 CEST 2007


Lawrence Oluyede wrote:
> I'd like to know if there's a practical way to resolve all the
> references in a RelaxNG document. Maybe 'expand' is the correct word.
> 
> I have a document with some <define> which are mostly custom data
> types and some elements using those types. For example:
> 
> ----
> <define name="foo">
>   <choice>
>     <value>a</value>
>     <value>b</value>
>   </choice>
> </define>
> 
> <define name="ABC">
>   <element name="abc">
>     <ref name="foo">
>   </element>
> </define>
> ---
> 
> What I'd like is to replace the reference "foo" with the definition of
> "foo"  as a whole.

I think the easiest way is to do it by hand, something like:

    resolve = etree.XPath("//rng:define[@name = $name]", namespaces=...)
    for ref in tree.iter("ref"):
        define = resolve(tree, name = ref.get("name"))
        if define:
             ref.getparent().replace(ref, define[0]) # or define[0][0] ?

Stefan



More information about the lxml-dev mailing list