views:

26

answers:

2

I want to define an XML schema which allows elements like this:

<Element>TYPE1</Element>
<Element>TYPE2</Element>
<Element>TYPE3</Element>
<Element>Any Other Text</Element>

In this case, TYPE1, TYPE2 and TYPE3 are enumerated strings. However, the schema also allows any text content in Element.

This seems like a strange restriction. Here is why I want the type to be a pseudo-enumeration. I want it to be an enumeration so that autocomplete (called intellesense in Visual Studio) will work in XML editors. However, the enumeration list isn't a hard constraint because the software (Java) ingests the XML document using JAXB and is robust to values that aren't enumerated.

I can provide more details if they are needed.

+1  A: 

As far as I know, there isn't a way to accomplish what you are trying to do. Enumerated type restrictions in xsd's are hard constraints. Otherwise, you would be using an xsd:string type which doesn't have any restrictions.

btreat
+1  A: 

The short answer is, as btreat already said, that this ain't possible - nor in schema not in any other formal description of content (that I can think of)).

How would you imagine it to work? Schemas are formal, restrictive, documents that describe content (which can be validated based on that description). Enumeration is based on assumption that the values are known and well defined. If you add a wildcard option (in case of schema it could be xsd:string) then whats the difference between the wildcard and the enumerated values form validator perspective? Thus it's forbidden by design.

The only similar structure, and only in programming languages not content descriptors, to what you propose is a switch statement with default block but it's purpose is different - it defines alternative execution paths rather then alternative content.


In case of requirement to validate different values accumulated over time I would keep older schema and document versions and have each version valid with it's corresponding in time/version restrictions. Thus new documents will always be valid with current schema and you retain the ability to work (and check) older ones if need arises.

Zergin
Thank you for an insightful response. Let me describe my app to illustrate why I want this. In my app, a user is building document which abides by various restrictions. They fill out a series of data fields some of which hold enumerated types. For enumerated types values come from a pull down menu. Over various versions of the application, the values in the enumeration will change (some added some deleted). We want to persist legal and illegal values while maintaining a descriptive schema.We want a content description mechanism which encodes restrictions, but is robust to violations.
Spina
Doesn't it defy the restrictions to have them ignored by design? In your case I would keep older schema and document versions and have each version valid with it's corresponding in time/version schema. Thus new documents will always be valid with new restrictions but you have the ability to work (and check) older ones if need arises. Your version seems to me like the classic example of wanting to eat the cookie and have the cookie ;-)
Zergin