views:

122

answers:

1

I want to use RELAX NG compact syntax to validate an XML element whose children are one, two, three or n of a set of n specific elements. For instance, if the element is 'Layout' and there is a set of three specific elements: 'top', 'center' and 'bottom', the following XML element definitions would be valid:

<Layout>
    <top/>
    <center/>
    <bottom/>
</Layout>

<Layout>
    <top/>
</Layout>

<Layout>
    <center/>
</Layout>

<Layout>
    <bottom/>
</Layout>

<Layout>
    <top/>
    <center/>
</Layout>

I want to know how to write two patterns: 1) Allow the children to be in any order. 2) Restrict the children to be in a specific order (e.g.: top, center, bottom).

The solution I have so far for the XML example and the ordered pattern is:

element Layout {
   (
      element top { text },
      element center { text }?
   ) |(
      element top { text }?,
      element center { text }?,
      element bottom { text }
   ) |(
      element center { text }
   )
}

I don't have a good solution for more than 3 elements and/or for an unordered pattern.

+1  A: 

For unordered elements, have you looked at the interleave connector?

element Layout {
    element top { text } &
    element center { text } &
    element bottom { text }
}

For restricting the order for a set of n elements, I'm wondering if this is even a job for schema validation or should the responsibility be shifted to whatever tool is displaying/printing/etc. the data?

Doug Hurst