Reputation: 836
I defined the XSD scheme for my custom XML (which I use for files generation). I've got an attribute named type
. I want that it accepts a predefined list and also any other string value. Because the type
can be standard (int, string, etc.) and also custom (any name). Is it possible?
Example of the XML:
<submodel name="Country">
<field name="Handler" type="Clients.CountryHandlerModel" />
<field name="Name" type="string" />
</submodel>
And the XSD description for the type
attribute:
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction>
<xs:simpleType>
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="bit"/>
<xs:enumeration value="boolean"/>
<xs:enumeration value="int"/>
<xs:enumeration value="decimal"/>
<xs:enumeration value="double"/>
<xs:enumeration value="date"/>
<xs:enumeration value="datetime"/>
<xs:enumeration value="string"/>
<xs:enumeration value="binary"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
Upvotes: 1
Views: 955
Reputation: 15337
you can accomplish to have a string field that you give the user some option as way to improve the Discoverability
as well as any value the user wants by using union
here is my example where user can choose a predefined lang-validation, or any type
<xsd:simpleType name="system_languages_plus">
<xsd:union>
<xsd:simpleType> <!--the current implemented validations -->
<xsd:restriction base="xsd:string">
<xsd:enumeration value="html" />
<xsd:enumeration value="json" />
<xsd:enumeration value="xml" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType> <!--the user can add his own validation -->
<xsd:restriction base="xsd:string">
</xsd:restriction>
</xsd:simpleType>
</xsd:union>
</xsd:simpleType>
Upvotes: 0
Reputation: 23001
There's no point in having a specific list of values if you also accept any other value as valid too.
Let's imagine you are a schema validator, trying to validate whether a value correctly matches this type. You could go through the predefined list looking for a match, but what happens when the value isn't in that list? You accept it anyway, because the schema considers any other string value to be valid too. So there was really no point in checking the list.
Just make the type xs:string
or xs:token
and leave it at that.
Now Michael Kay had made the argument that a more complex data type (a union of your enumeration with a string) might still be of some use if you're using the schema for data typing. Such a schema might look something like this (just showing the contents of the xs:list
element to keep things simple):
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="bit"/>
<xs:enumeration value="boolean"/>
<xs:enumeration value="int"/>
<xs:enumeration value="decimal"/>
<xs:enumeration value="double"/>
<xs:enumeration value="date"/>
<xs:enumeration value="datetime"/>
<xs:enumeration value="string"/>
<xs:enumeration value="binary"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:token"/>
</xs:simpleType>
</xs:union>
</xs:simpleType>
But I still don't think that's going to be of any use to you.
Say you wanted to convert that xsd into a class for the purpose of serializing objects to XML or vice versa. I expect you'd use something like Microsoft's xsd utility, given that you're using Visual Studio. But that's going to take this massively complex type definition and just treat it as a simple string. Again there's no benefit.
Having said that, I'll concede that there may well be other schema processing tools, or other situations where a complex data type may be of some use. But until someone provides a real world example of such a use, I still believe a simple list of strings or tokens is the most sensible solution.
Upvotes: 0
Reputation: 163438
Although James Holderness is right if you look on validation purely as a yes/no function, if you want to do data typing then a schema that defines a union type whose members are (a) a type containing a list of permitted values, and (b) xs:string can sometimes be useful, because the type annotations attached to the nodes will reflect whether the value was present in the list or not. But you need to be clearer about why you want this.
Upvotes: 2