How to use any string in xsd: enumeration

I have this type of xsd

<xsd:simpleType name="carsEnum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Seat"/> <xsd:enumeration value="Opel"/> </xsd:restriction> </xsd:simpleType> 

Now I can use it this way - <xsd:attribute name="carModel" type="carsEnum"/>

How can I rebuild carEnum to use any other string?

As an example -

 < ... carModel="Seat"/> < ... carModel="Some string"/> < ... carModel="Opel"/> 

Due to the reason, I can make carsEnum type, as usual, String, but it is quite convenient to use such a construction in Idea IDE, because it shows tool tips.

+6
source share
2 answers

To summarize your question, I understand that you want to keep a list of the possible values โ€‹โ€‹of elementary cars, also want to accept any values โ€‹โ€‹that appear outside this limited list. This can be achieved in XSD using UNION . I illustrated this with the example below.

XML sample:

 <?xml version="1.0" encoding="utf-8"?> <cars>ssd</cars> 

XSD:

 <?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="cars" type="carsType"/> <xs:simpleType name="carsType"> <xs:union memberTypes="carsEnum carsAnyString"/> </xs:simpleType> <xs:simpleType name="carsEnum"> <xs:restriction base="xs:string"> <xs:enumeration value="Seat"/> <xs:enumeration value="Opel"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="carsAnyString"> <xs:restriction base="xs:string"/> </xs:simpleType> </xs:schema> 

In the above XSD, I use several CAR definitions, once as a list of enumerations and once as any line. The definition of the UNION type that combines the two will be type for cars.

So, <cars can have values โ€‹โ€‹such as: Seat, Opel, anyOtherCar, AnyString2 ..

I would also like to mention a way to control the value of ANY STRING . Above XSD can accept any string, which means even special characters and numbers. We can limit this by adding a restriction pattern only to Alpha characters. The following is the XSD code:

 <?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="cars" type="carsType"/> <xs:simpleType name="carsType"> <xs:union memberTypes="carsEnum carsAnyAlphaString"/> </xs:simpleType> <xs:simpleType name="carsEnum"> <xs:restriction base="xs:string"> <xs:enumeration value="Seat"/> <xs:enumeration value="Opel"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="carsAnyAlphaString"> <xs:restriction base="xs:string"> <xs:pattern value="[A-Za-z]*"/> </xs:restriction> </xs:simpleType> </xs:schema> 

Thus, the possible values โ€‹โ€‹can be Seat, Opel, "Any string but no Number", "Any string but no special char"

replacement

  `<xs:pattern value="[A-Za-z]*"/>` 

by

  <xs:pattern value="[A-Za-z]+"/> 

does not allow an empty string. This is a way to override an item. Don't just stick to the list of listings.

You now have an enumerated template. Hope this helps.

+8
source

Just add other elements like this.

 <xsd:simpleType name="carsEnum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Seat"/> <xsd:enumeration value="Opel"/> <xsd:enumeration value="SeatNew"/> <xsd:enumeration value="OpelNew"/> </xsd:restriction> </xsd:simpleType> 

as a result, you can assign SeatNew and OpelNew to your carModel.

However, you cannot assign an arbitrary value to carModel unless it is specified in the file, as it will be converted to a java enumeration.

+1
source

All Articles