views:

24

answers:

1

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.

+1  A: 

There really isn't a terribly non-clunky way of doing this in RELAX NG I'm afraid.

I'd take one of two approaches. Either build up patterns or use Schematron annotations and process in an environment where you can do process using both languages:

For example, building up groups of B elements:

<define name="B2">
    <ref name="B"/>
    <optional><ref name="B"/></optional>
</define>

<define name="B4">
    <ref name="B2"/>
    <ref name="B2"/>
</define>

letting you slowly build up groups of (in this case), one or two elements , then 2-4 elements and so on. Combining different groups will let you specify the appropriate count (eventually).

Alternatively you could use Schematron annotations:

<?xml version="1.0" encoding="utf-8"?> 
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:s="http://www.ascc.net/xml/schematron"&gt; 

    <element name="A">
      <s:rule context="A">
        <!-- note - using XPath 2 here -->
        <s:assert test='count(B) lt 6'>A elements may contain no more than 6 B elements</s:assert>
      </s:rule>
      <ref name="B"/>
      <oneOrMore><ref name="B"/></oneOrMore>
    </element>

    <define name="B">
      <element name="B">
        <text/>
      </element>
    </define>

</grammar>

I tend to prefer the latter as it makes for much simpler schemas and it's not hard to intermix the two but it does depend on your validation environment.

Nic Gibson
Thanks for the suggestions! I had thought of your first approach, but decided against it. The second approach seems like it wouldn't be too much trouble. I'll probably end up using this.
A. Levy