Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML Schema (XSD): allow an attribute OR a sub-element NOT BOTH

Reply
Thread Tools

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

 
 
Eric
Guest
Posts: n/a
 
      03-07-2007
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>

 
Reply With Quote
 
 
 
 
usenet@tech-know-ware.com
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 18:46, "Eric" <eml...@hotmail.com> wrote:
> 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/
=============================================

 
Reply With Quote
 
 
 
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-07-2007
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
 
Reply With Quote
 
Stan Kitsis [MSFT]
Guest
Posts: n/a
 
      03-07-2007
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
"Eric" <> wrote in message
news: ps.com...
> 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>
>



 
Reply With Quote
 
Stan Kitsis [MSFT]
Guest
Posts: n/a
 
      03-07-2007
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
"Stan Kitsis [MSFT]" <> wrote in message
news:45ef4723$...
> 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
> "Eric" <> wrote in message
> news: ps.com...
>> 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>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM
XML Schema to XML Schema Conversion Hari Om XML 1 03-02-2004 09:04 PM
XML schema regular expressions question and recommended XML Schema book Fred Smith XML 1 02-05-2004 11:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57