views:

76

answers:

2

I am trying to allow for two attribute values for one element. This is how I have it done now, but I only want to allow 1 of the 2, but not both.

<contact phoneH="cell">8025550000</contact>
<contact phoneW="work"> 8025550094</contact>

I am looking to be able to do something like this:

<xsd:attribute name="phoneH|phoneW" type="xsd:string" />

Is there a way to do use the restriction element with attributes?

Thanks,
Levi

+1  A: 

I don't believe that attribute constraint is possible with XML Schema.

You can apply additional validation mechanisms (i.e. schematron) or use a different schema implementation (i.e. Relax-NG)

http://lists.xml.org/archives/xml-dev/200107/msg00070.html

Unfortunately you can't do this with W3C XML Schema. As I see it you have two options:

1) Change to a different schema dialect (I think RELAX-NG support this functionality) 2) Express you co-constraint using an embedded schematron rule

These constraints can be validated with the new Schematron Validator from Topologi. The tool is free and can be downloaded from www.topologi.com.

Mads Hansen
Ok, I will have to do some reading on both of those. I know at some point this semester my XML teacher is going to go over them, but it might be good to have some basics down before that.
Levi
+1  A: 

You can't do this with W3C xsd.

An alternative would be making phoneH and phoneW subelements of contact, and using xs:choice to limit the user to one or the other subelement.

womp
Ok. I had actually just read about the second option and I would normally just switch over and do it that way but this is part of some non-graded homework so I didn't want to deviate too far from the requirements. But yeah, choice is a good alternative for a normal scenario. Thanks!
Levi