RelaxNG: <choice> containing <zeroOrMore>

530 Views Asked by At

In RelaxNG, I want to describe a structure similar to this:

<parent>
    <subelem1>
    <subelem1>
    <subelem1>
    ...
</parent>

or:

<parent>
    <subelem2>
    <subelem2>
    <subelem2>
    ...
</parent>

I tried with the following rng:

<rng:element name="parent">
    <rng:choice>
        <rng:zeroOrMore>
            <rng:ref name="subelem1"/>
        </rng:zeroOrMore>
        <rng:zeroOrMore>
            <rng:ref name="subelem2"/>
        </rng:zeroOrMore>
    </rng:choice>
</rng:element>

But verifying with lxml in Python, I only get error messages Did not expect element subelem1 there.

What's wrong there?

1

There are 1 best solutions below

2
On BEST ANSWER

I've used a slightly different schema to yours but it should be functionality the same:

<rng:grammar 
    xmlns:rng="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <rng:start>
        <rng:ref name="dparent"/>
    </rng:start>

    <rng:define name="dparent">
    <rng:element name="parent">
        <rng:choice>
            <rng:zeroOrMore>
                <rng:element name="subelem2">
                    <rng:empty/>
                </rng:element>
            </rng:zeroOrMore>
            <rng:zeroOrMore>
                <rng:element name="subelem1">
                    <rng:empty/>
                </rng:element>
            </rng:zeroOrMore>
        </rng:choice>
    </rng:element>

    </rng:define>

That validates both your sample documents just fine using jing and also with xmllint (which uses libxml2 as does lxml in python if I remember correctly). I'd suggest comparing your full grammar (I assume you only posted part of it) with the above. Also, I corrected your namespaces (the choice element wasn't in one). If you still can't validate I would suggest hte problem is with lxml rather than the grammar.