XSD question

D

David Kastrup

Hi,

not sure this is the right group (better ideas?), but anyway: is it
possible in an xsd Scheme to specify that some element must have
exactly one of _either_ an attribute called "name" or an element
called "name"?

I've been digging through some books but am not clueful enough to come
up with an answer. It feels like it should be an obvious "you do this
that way" or "you can't do this with XSD", but I am lacking the
expertise to figure out which it is.
 
J

Joseph Kesselman

David said:
exactly one of _either_ an attribute called "name" or an element
called "name"?

To do that in XML Schema, you'd move back up a level, and define the
element as being one of two distinct types -- one with the element and
one with the attribute.
 
D

David Kastrup

Joseph Kesselman said:
To do that in XML Schema, you'd move back up a level, and define the
element as being one of two distinct types -- one with the element and
one with the attribute.

I guess this is not really feasible: usually there is more than one
such attribute, so one would get an exponential explosion of types.

So we'll likely have to declare both attribute and element optional
and barf at a later stage of processing.

Thanks,
 
S

Stan Kitsis [MSFT]

Hi David,

If it's just an element/attribute pair where you can only have one but not
the other, you can achieve it with a uniqueness constraint. The example
below allows you to have either attr1 or elem1 or neither, but not both. If
you want to require that exactly one (attribute or element) is present,
replace unique with key.

<xs:element name="node">

<xs:complexType>

<xs:sequence>

<xs:element name="elem1" type="xs:string" minOccurs="0"/>

</xs:sequence>

<xs:attribute name="attr1" type="xs:integer" use="optional"/>

</xs:complexType>

<xs:unique name="element_or_attribute">

<xs:selector xpath="."/>

<xs:field xpath="@attr1|myNS:elem1"/>

</xs:unique>

</xs:element>


This example assumes that you have a target namespace defined and that it's
mapped to myNS prefix.
--
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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top