RE: Defining unique pair of elements

Hello Jeni,

thanks for the help.

> 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.

How can I restrict that there is just one element of type masterAgentModule 
available and 0 or more of type proxyAgentModule. I have defined next:

<xsd:complexType name="moduleType">
	<xsd:sequence>
		...
	</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="masterAgentModule">
	<xsd:complexContent>
		<xsd:extension base="moduleType"/>
			<xsd:sequence>
				...
			</xsd:sequence>
		</xsd:extension>
	</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="proxyAgentModule">
	<xsd:complexContent>
		<xsd:extension base="moduleType"/>
			<xsd:sequence>
				...
			</xsd:sequence>
		</xsd:extension>
	</xsd:complexContent>
</xsd:complexType>

<xsd:element name="node">
	<xsd:complexType>
		<xsd:sequence>
			???
		</xsd:sequence>
	</xsd:complexType>
</xsd:element>

Kind regards,
Ismaël

-----Original Message-----
From: Jeni Tennison [mailto:jeni@jenitennison.com]
Sent: woensdag 19 maart 2003 17:27
To: Cams Ismael
Cc: xmlschema-dev@w3.org
Subject: 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 Thursday, 20 March 2003 04:29:34 UTC