views:

108

answers:

3

Is it possible using XSD to restrinct node names to enumeration, and then based on this enumeration add another restrictions?

In example, I have this xml:

<a>
    <b name="string" value="hello">
    <b name="integer" value="123">
</a>

I want "b" nodes have name attribute from enumeration { "string", "integer" }. Then if it's "string" I want that "value" attribute to be type of xs:string, and if it's "integer" I want that "value" attribute to be type of xs:integer.

+1  A: 

You can do certain limit, e.g. you can limit that the values of your name attribute come from a given list - but you cannot express these kind of relations between "if name is string, then the type of value must be xs:string" in the XML schema.

You'll have to either use some other technique (like Schematron), or check this in your app code.

Marc

marc_s
Thanks for the response. I'll look closer to schematron.
ppiotrowicz
A: 

No. But you're treating name like a type - and XML Schema does have some support for the complexType of an element being determined by a string value. However, you have to use the attribute name "xsi:type" in your XML document, so it would look like this:

<a>
    <b xsi:type="string" value="hello">
    <b xsi:type="integer" value="123">
</a>

That's the best that XML Schema can do I'm afraid. More details in the official primer (which can be pretty confusing, unfortunately): http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs

13ren
+1  A: 

No. You can't do this in XSD. In essence, you have 2 <b>s with different types. This violates the Element Consistency rule.

You have a few options,

  1. Enforce the rules outside schema, in your application. This is what I will do.
  2. Use a validation language like Schematron, as mentioned by others.
  3. Switch to a more powerful schema language like Relax NG.
ZZ Coder
Just as I thougth. Thanks for your answer.
ppiotrowicz