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

Eric (archive)

Usenet Archive
Joined
Jan 17, 2000
Messages
477
Reaction score
0
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>
 

usenet

Usenet Archive
Joined
Aug 10, 2000
Messages
1,105
Reaction score
0
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.
 

Joseph Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
733
Reaction score
0
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.
 

Stan Kitsis MSFT

Usenet Archive
Joined
Dec 15, 2004
Messages
64
Reaction score
0
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 MSFT

Usenet Archive
Joined
Dec 15, 2004
Messages
64
Reaction score
0
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.
 

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
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top