Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Eric
Guest
 
Posts: n/a
#1: Mar 7 '07
Attached is an example of my question. Note the "values" attribute is
optional. Also the <valuesub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <valuesub-elements - but not both.

I tried using a <choicewith 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@tech-know-ware.com
Guest
 
Posts: n/a
#2: Mar 7 '07

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


On 7 Mar, 18:46, "Eric" <eml...@hotmail.comwrote:
Quote:
Attached is an example of my question. Note the "values" attribute is
optional. Also the <valuesub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <valuesub-elements - but not both.
>
I tried using a <choicewith 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/
=============================================

Joseph Kesselman
Guest
 
Posts: n/a
#3: Mar 7 '07

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


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.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Stan Kitsis [MSFT]
Guest
 
Posts: n/a
#4: Mar 7 '07

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


You can do what you want by adding the following uniquness constraint to the
<characteristicelement (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
"Eric" <emleml@hotmail.comwrote in message
news:1173293194.874932.29310@h3g2000cwc.googlegrou ps.com...
Quote:
Attached is an example of my question. Note the "values" attribute is
optional. Also the <valuesub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <valuesub-elements - but not both.
>
I tried using a <choicewith 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>
>

Stan Kitsis [MSFT]
Guest
 
Posts: n/a
#5: Mar 7 '07

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


One thing I forgot to mention is that <xs:uniquedoes 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:keyinstead 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
"Stan Kitsis [MSFT]" <skits@microsoft.comwrote in message
news:45ef4723$1@news.microsoft.com...
Quote:
You can do what you want by adding the following uniquness constraint to
the <characteristicelement (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
"Eric" <emleml@hotmail.comwrote in message
news:1173293194.874932.29310@h3g2000cwc.googlegrou ps.com...
Quote:
>Attached is an example of my question. Note the "values" attribute is
>optional. Also the <valuesub-element is optional. Here, the XML
>can contain, 1 or both or neither. I would like to allow EITHER the
>"values" attribute or <valuesub-elements - but not both.
>>
>I tried using a <choicewith 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>
>>
>
>

Closed Thread