xml schema null or double type

H

hello

How can I define the schema so that myage element has to be double or
null?

<xs:simpleType name="myage">
<xs:restriction base="xs:double">
<xs:enumeration value="null"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
 
G

George Bina

Hi,

You can define that as a union between double and a type that allows
only the null value:

<xs:simpleType name="myage">
<xs:union memberTypes="xs:double">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="null"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>

However, XML Schema has support for nillable elements, so if you want
to use this simple type for an element you might consider the
alternative of defining the element nillable. Then in the instance you
just need to add xsi:nil="true" to the element to mark it as having no
value.

Best Regards,
George
 
S

shuan

when i use nill=true for the particular element i got the following error.

org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '' is not a valid
value
for 'double'.

when i define the union like this.

<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="null"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:double" />
</xs:simpleType>
</xs:union>
</xs:simpleType>

i got this error.

org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.3: '' is not a valid
value
of union type '#AnonType_iims11526672818165646iims11526672557483110'.

probably i am missing something.

SK
 
G

George Bina

Hi,

In the first case you need
xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
otherwise you just add an attribute from no namespace with the same
local name as the nil attribute from the schema instance namespace.

In the second case I thoght you want the string "null" as value when
you said double or null. If you want the empty string as possible value
replace null from the enumeration value with an empty value:
<xs:enumeration value=""/>

Best Regards,
George
 
S

Shuan

Thanks, George,
In the first case you need
xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
otherwise you just add an attribute from no namespace with the same
local name as the nil attribute from the schema instance namespace.
I am including this line.
In the second case I thoght you want the string "null" as value when
you said double or null. If you want the empty string as possible value
replace null from the enumeration value with an empty value:
<xs:enumeration value=""/>
Event I set to value="" I still get the same error.
 
G

George Bina

Hi,

Here it is the full working example:


<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="test">
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:double"/>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
</xs:schema>

<test></test>

should be reported valid against the above schema.

Best Regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top