Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD - Optional Integer

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
0
0
GB
Could someone please point me in the right direction.

I have an XSD file which declares an element of type Location:
Code:
<xs:element name="location">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="autologid" type="optionalIntType"/>
      <xs:element name="locationname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Note, the autologid element is of type optionalIntType which is intended to restrict values to integer types but allow empty values. I.e. an empty string or an integer.

Code:
<xs:simpleType name="optionalIntType">
  <xs:union memberTypes="xs:integer">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:length value="0"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>

Unfortunately when I run my XML through he XSD I get an exception:

Code:
Child node autologid below parent node location does not contain a value

Can anyone tell me how to declare my optionalIntType type so that it allows either an empty value or an integer please ?

Thanks
Smeat
 
It looks like acceptable. What validation engine are we talking about?
 
Hi tsuji

You are right.

The problem was in my validation logic.

Thanks for pointing that out

Smeat
 
If you want to let the freedom of empty element as a proxy of value 0, you can use the attribute default to acheive that.
[tt] <xs:element name="autologid" type="xs:integer" default="0" />[/tt]
That looks simpler and even acheive a more definite functional purpose for applications implemented validation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top