Altova Mailing List Archives


Re: [xml-dev] modelling the business rule "at least one of the elements"

From: "Rick Jelliffe" <rjelliffe@-------.---.-->
To: xml-dev@-----.---.---
Date: 11/22/2006 5:22:00 AM
(resent)

XSD provides a content type <ALL> which does what you need, I think.

If you wanted the constraint that you could have more than one of each,
but at least one of each, you would be looking at a really nasty content
model, something like the following:

  (a+,
          (b,
                   (a, (a |b)*)?, c)
                 | (b, (a |b)*)?, c)
                 |  c )
         |
          (c,
                   (a, (a |c)*)?, b)
                 | (c, (a |c)*)?, b)
                 |  b ))
|
   (b+,

          (a,
                   (a, (a |b)*)?, c)
                 | (b, (a |b)*)?, c)
                 |  c )
         |
          (c,
                   (b, (b |c)*)?, a)
                 | (c, (b |c)*)?, a)
                 |  a ))
|
   (c+,

          (a,
                   (a, (a |c)*)?, b)
                 | (b, (a |c)*)?, b)
                 |  b )
         |
          (b,
                   (b, (b |c)*)?, a)
                 | (c, (b |c)*)?, a)
                 |  a )),

  (a | b | c)*
which would take about 125 XSD elements, I think.

I think XSD 1.1 improves ALL to cope with this situation better.

Cheers
Rick Jelliffe

N.B. Using Schematron, you would say for the ALL case

<rule context="parent">
  <assert test="count(a)=1">Parent must have one child a</assert>
  <assert test="count(b)=1">Parent must have one child b</assert>
  <assert test="count(c)=1">Parent must have one child c</assert>
  <assert test="count(*) = count(a) + count(b) + count(c)">
   The parent  can only contain a, b and c elements
  </assert>
</rule>

And the equivalent Schematron schema for the 125 element XSD rule is

<rule context="parent">
  <assert test="count(a) &gt; 0">Parent must have at least one child
a</assert>
  <assert test="count(b) &gt; 0">Parent must have at least  one child
b</assert>
  <assert test="count(c) &gt; 0">Parent must have at least  one child
c</assert>
  <assert test="count(*) = count(a) + count(b) + count(c)">
   The parent  can only contain a, b and c elements
  </assert>
</rule>

125 versus 5...

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.