I am developing a schema using RELAX NG. I'm pretty new to this, so it is quite possible that I am overlooking something obvious, but there doesn't seem to be a convenient way to specify the number of occurrences of an element like you can in the XML Schema language.
For example, suppose I wanted to specify that an A
element may contain 2 - 5 B
elements. I don't want to use the zeroOrMore
or oneOrMore
tags because I actually do have an upper bound on the number of elements. In XML Schema, I can use the minOccurs
and maxOccurs
properties to specify that compactly. I've read through the official RELAX NG tutorial, skimmed the spec, and done some basic googling, but I haven't been able to find any convenient way to do this with RELAX NG. The best I can figure out, you would need to do something like this:
<element name="A">
<ref name="B"/>
<ref name="B"/>
<optional><ref name="B"/></optional>
<optional><ref name="B"/></optional>
<optional><ref name="B"/></optional>
</element>
<define name="B">
<element name="B">
<text/>
</element>
</define>
This is doable, but will start looking ugly when you need a larger number of occurrences. In my actual schema, I have one element type which could possibly occur up to 256 times, so the manually specified optional elements will be clunky. I'll do it if I need to, but I'd like to know if there is a more elegant way of expressing my occurrence restrictions.