XSD question - relationship between attributes?

Stewart Johnson

Usenet Archive
Joined
Nov 22, 2004
Messages
1
Reaction score
0
Hey All -

I'm stuck while trying to write a schema, hoping someone can provide me with a flash of inspiration.

One of the elements I'm writing has two attributes, both of which are optional. I want to express in the schema that there's a relationship between the two -- that the second attribute is optional only if the first one is not specified. ie:

<myElement /> <!-- valid -->
<myElement name="A" /> <!-- valid -->
<myElement myattr="true" /> <!-- invalid because name wasn't specified
-->
<myElement myattr="true" name="A" /> <!-- valid -->

So far my schema looks like this:

<element name="myElement">
<complexType>
<attribute name="name" type="string" use="optional" />
<attribute name="myattr" type="boolean" use="optional"
default="false" />
</complexType>
</element>

That captures the default values and the optional nature of the two elements, but it doesn't capture the relationship between the two. Is there any xsd construct I can use to express the relationship? I know how to do it with elements (xsd:group, xsd:choice) but not with attributes.

Thanks for any thoughts.

Stewart
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Stewart Johnson wrote:

I'm stuck while trying to write a schema, hoping someone can provide me with a flash of inspiration.

One of the elements I'm writing has two attributes, both of which are optional. I want to express in the schema that there's a relationship between the two -- that the second attribute is optional only if the first one is not specified. ie:

<myElement /> <!-- valid -->
<myElement name="A" /> <!-- valid -->
<myElement myattr="true" /> <!-- invalid because name wasn't specified
-->
<myElement myattr="true" name="A" /> <!-- valid -->

So far my schema looks like this:

<element name="myElement">
<complexType>
<attribute name="name" type="string" use="optional" /> <attribute name="myattr" type="boolean" use="optional" default="false" /> </complexType> </element>

That captures the default values and the optional nature of the two elements, but it doesn't capture the relationship between the two. Is there any xsd construct I can use to express the relationship? I know how to do it with elements (xsd:group, xsd:choice) but not with attributes.

I don't think that a W3C XML schema can express such constraints between attributes. Other schema languages are more powerful in that regard, for instance schematron should allow that.
 

Stone

Usenet Archive
Joined
Mar 18, 2002
Messages
36
Reaction score
0
Martin Honnen said:
Stewart Johnson wrote:

I don't think that a W3C XML schema can express such constraints between attributes. Other schema languages are more powerful in that regard, for instance schematron should allow that.

Hi,

I don't think you can do it using attributes. But you can express it using elements : use a <xs:choice> within a <xs:sequence>
 

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,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top