W3C home > Mailing lists > Public > xmlschema-dev@w3.org > March 2003

Re: Defining unique pair of elements

From: Jeni Tennison <jeni@jenitennison.com>
Date: Wed, 19 Mar 2003 16:26:40 +0000
Message-ID: <155965243396.20030319162640@jenitennison.com>
To: Cams Ismael <Ismael.Cams@siemens.com>
CC: xmlschema-dev@w3.org

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.

to test the constraint.

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

<element name="modules">
    <element name="module">
      <element name="masteragent">...</element>
      <element name="module">
        <element name="proxyagent">...</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



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

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:09 UTC