I have such xsd type
<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 in such way - <xsd:attribute name="carModel" type="carsEnum"/>
How I can rebuild carEnum to use any another string?
As an example -
< ... carModel="Seat"/>
< ... carModel="Some string"/>
< ... carModel="Opel"/>
Of cause I can make type carsEnum as usual String, but it's rather comfortable to use such construction in IDE Idea, because it show tool tips.
If I summarize your question what I understand is you want to maintain a list of possible values of an element cars, also want to accept any values appearing outside that bounded list. This can be achieved in XSD using UNION
. I have illustrated it with an example below.
sample XML:
<?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 am using multiple definitions of CAR, once as enum list and once as any string. Defining a UNION type combining these two, will be the type
for cars.
So <cars
can have values like:
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 that means even special chars and numbers. We can restrict this by adding restriction pattern
to only Alpha chars. Below 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>
So possible values can be Seat, Opel, "Any string but no Number", "Any string but no special char"
replacing
`<xs:pattern value="[A-Za-z]*"/>`
by
<xs:pattern value="[A-Za-z]+"/>
doesn't allow null string. This is a way to redefine an element.. not being just stick to the enumeration list.
Now you have pattern also with enumeration list. Hope it helps.
Just add the 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 will be able to assign SeatNew and OpelNew to your carModel.
However you can not assign any arbitrary value to carModel unlesss specified in the since it will be converted to java enum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With