My problem (or question) centers around empty elements which are typed as xs:integer. I need to allow for empty elements so I used a union to allow an empty element or a valid integer as the value as shown in the schema below. However, my schema serves a dual role and also needs to be imported into 3rd party software which expects data types of String, Float, Integer or Date. If I code the schema using the union method for all integers they will not be typed as integers in the software. Is there another way other than the union method of allowing an empty element for integer data types? I'd like to just have the one XSD but can have two if that is what needs to happen.
Given XML sample of:
<?xml version="1.0" encoding="UTF-8"?>
<company>
<division>
<department>
<roles/>
<employees>7</employees>
</department>
</division>
</company>
And schema of:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="nullval">
<xs:union memberTypes="IntegerType empty"/>
</xs:simpleType>
<xs:simpleType name="IntegerType">
<xs:restriction base="xs:integer"/>
</xs:simpleType>
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:maxLength value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="company">
<xs:complexType>
<xs:sequence>
<xs:element name="division">
<xs:complexType>
<xs:sequence>
<xs:element name="department" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<!-- elements may appear in any order -->
<xs:all minOccurs="0" maxOccurs="1">
<xs:element name="roles" type="nullval"/>
<xs:element name="employees" type="xs:integer"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Answers. The proper way with the W3C XML schema language is using nillable="true" in the schema and xsi: nil="true" in the XML instance document e.g.
For brevity, the text and examples in this specification use the prefix xs: to stand for this namespace; in practice, any prefix can be used. in the end xs or xsd are only prefixes. XSD is used for example more by Microsoft schemas. The important is how you declare the namespace.
The presence of the xsd:nillable attribute in an XSD element means that the corresponding element in the XML file permits null values.
XSD Restrictions/Facets Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
This data type also supports these special values: not-a-number that represented as NaN, and positive and negative infinity that is represented as INF and -INF. The Registry Services application always uses a canonical representation when an xsd:float data type value is used in this application output.
The reason for the error you got is that null is not a valid value per W3 specs for a xs:date. Thanks for the reply. But it does not seem to be working. Am I missing something I put <TrxDate xsi:nil="true"/> but still getting error. .. .. .. You are missing the xsi: prefix on nill. Here is my example again.
The XSD choice element extends the XML Schema definition. It provides a single or multiple choices of content elements in an arbitrary order. We describes how to use the choice element within XSDs and how to access the values in a JSP with EL. The <xsd:choice> -node can be used in the same way as a <xsd:sequence> to describe an OpenCms type.
The elements of a <xsd:choice> -node can be accessed in the same way as elements of the nested content. Before the content value is read, the existence of the root element and the choice elements have to be tested for their existence, e.g.:
Have you tried
<xs:element name="roles" type="xs:integer" nillable="true"/>
What you have to do is to assign two restrictions on the same element, plus make a union of them such as in the following example:
<xs:element name='job_code'>
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:length value='0'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base='xs:integer'>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
Using this restriction, you tell the XML validation to allow any integer value and allowing the element if it is empty.
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