Re: Defining unique pair of elements

Hi Ismaël,

> The combination module - masteragent must be present just one time.
> The combination module - proxagent may be available 0 or more times.
>
> How can I define this with xsd ?

You can't in any non-hacky way because XSD doesn't let two elements
with the same name in the same context have different types (content
models). You could do the following:

1. Add a fixed attribute to <masteragent> elements that always takes
the value 'master', then define an identity constraint that says every
<masteragent> element must have a unique value for that attribute.
This will ensure that the <masteragent> element can only appear once
if it appears at all, but doesn't ensure that the <masteragent>
element does appear somewhere.

2. Add a Schematron constraint, something like:

<sch:rule context="modules">
  <sch:assert test="count(module/masteragent) = 1">
    The masteragent element must appear once and only once.
  </sch:assert>
</sch:rule>

to test the constraint.

3. Switch to using RELAX NG, where you can just do:

<element name="modules">
  <interleave>
    <element name="module">
      <element name="masteragent">...</element>
    </element>
    <zeroOrMore>
      <element name="module">
        <element name="proxyagent">...</element>
      </element>
    </zeroOrMore>
  </interleave>
</element>

If you don't mind changing the way your instance looks, I suggest that
you either:

A. Define a moduleType type, and two derivations thereof --
masterAgentModule and proxyAgentModule. Then use xsi:type on
individual <module> elements to indicate whether they hold
<masterAgent> or <proxyAgent> elements.

B. Call the <module> element that holds the <masterAgent> something
other than "module", for example "masterModule".

For either of the two latter cases, you could always define a
transformation from the markup that's actually used to the validatable
elements.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Wednesday, 19 March 2003 11:26:56 UTC