XML Schema (XSD): allow an attribute OR a sub-element NOT BOTH

E

Eric

Attached is an example of my question. Note the "values" attribute is
optional. Also the <value> sub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <value> sub-elements - but not both.

I tried using a <choice> with two element definitions for
"characteristic" with different content, but this is clearly not
allowed in XSD.

Does anyone have an answer? Thanks.

========================================================================

<xs:element name="characteristics">
<xs:complexType>
<xs:sequence>
<xs:element name="characteristic">
<xs:complexType>
<xs:sequence>
<xs:element name="value" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="value" type="xs:string"
use="required" />
<xs:attribute name="description" type="xs:string"
use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN"
use="optional" />
<xs:attribute name="values" type="xs:string"
use="optional" />
<xs:attribute name="column" type="xs:NMTOKEN"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
 
U

usenet

Attached is an example of my question. Note the "values" attribute is
optional. Also the <value> sub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <value> sub-elements - but not both.

I tried using a <choice> with two element definitions for
"characteristic" with different content, but this is clearly not
allowed in XSD.

Alas this is not possible with XML schema 1.0. XSD 1.1 is expected to
address this as it is a common requirement, but there's no timetable
for that that I know of.

Some people embed schematron constraints into their schemas to address
this, but I don't know how good the tool support for this is.

Relax-NG should also be able to support this, but again tool support
may be an issue.

HTH,

Pete.
--
=============================================
Pete Cordell
Tech-Know-Ware Ltd
for XML to C++ data binding visit
http://www.tech-know-ware.com/lmx/
http://www.codalogic.com/lmx/
=============================================
 
J

Joseph Kesselman

The other current solution is to name the two versions differently and
make the content be a choice of one or the other of them. That's more of
a pain for the document and application authors, but it does accomplish
the goal.
 
S

Stan Kitsis [MSFT]

You can do what you want by adding the following uniquness constraint to the
<characteristic> element (before </xs:element>):

<xs:unique name="value_or_values">
<xs:selector xpath="."/>

<xs:field xpath="@values|mytns:value"/>

</xs:unique>

Note, this assumes that you have defined a target namespace for your schema
and its prefix is mytns. If you don't have the target namespace defined,
you will need to define it (and assign a prefix to it).

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
S

Stan Kitsis [MSFT]

One thing I forgot to mention is that <xs:unique> does not require you to
have at least one. It says that you can have one or the other or none, but
not both. If you want to require either the element or the attribute to be
present, use <xs:key> instead of <xs:unique> - everything else stays the
same.

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top