dragonfly
dragonfly

Reputation: 17773

XSD type validation according to one attribute value

As I remember, some time ago I had similar piece of XML:

<item type="Person" name="Pawel" Surname="Wu" />
<item type="Address" city="Sample town" address="Sample street etc." />

and I wantetd to validate it with XSD. However, last time I did it (about 2 years ago I had such a task) it was impossible to specify element type according to attribute value (type in this example). So I was forced to redesign XML structure into something like this:

<person name=..... />
<address city=..... />

and writing XSD was piece of cake.

Today, I have similar issue. As an input I get XML document with generic item elements that have type attriibute, and latter, depending on this attribute value, another attributes are allowed/required or not. Documents in that shape are still created in system manually, so I would like to have an XSD document that will reduce/eliminate errors (it's easy to forget that element item with type="personalDetails" must have a particular collection of attributes set).

Is it possible to write a XSD schema to validate such a documents now?

Thanks,Pawel

Upvotes: 2

Views: 2120

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

You can do this with XSD 1.1, currently implemented in Xerces and Saxon (i.e. not yet very widely).

One way to handle validation of a generic vocabulary like this is to transform it prior to validation, ie. convert <attribute name="size" value="23"> to <size>23</size>

Upvotes: 1

lexicore
lexicore

Reputation: 43651

You can't validate based on the attributevalue. However, you can use xsi:type to specify the type of the element in the XML instance. Click, click.

Upvotes: 2

Related Questions