XSD: default integer value range
Asked Answered
Z

1

11

Is there an implied default value range when defining an element of a specific data type in an XSD file? For example if I define an element of type integer:

<xs:element name="MyIntegerElement" type="xs:integer"/>

Does this have an implied min and max value that it will validate to? I know I can explicitly define the valid ranges like so:

<xs:element name="MyIntegerElement">
   <xs:simpleType>
      <xs:restriction base="xs:integer">
         <xs:minInclusive value="1"/>
         <xs:maxInclusive value="16"/>
      </xs:restriction>
   </xs:simpleType>
</xs:element>

But if I don't do this when I validate an XML file against this will it default to a range of valid values? I've been digging around in the XSD documentation but haven't found the answer yet.

Zitella answered 18/3, 2013 at 20:26 Comment(0)
W
8

Well, it depends on the data type...

If you look at the definition of integer at w3:

The value space of integer is the infinite set {...,-2,-1,0,1,2,...}

In essence it means that, for integers, by default there is no min/max value range since any integer can be represented.

On the other hand, for an int:

(...) maxInclusive to be 2147483647 and minInclusive to be -2147483648.

The list goes on for longs, shorts, etc...

You can read it in more detail here: http://www.w3.org/TR/xmlschema-2/#typesystem

Winterize answered 18/3, 2013 at 20:49 Comment(6)
Yes but at that point you are explicitly defining the valid range using maxInclusive and minInclusive. If I don't explicitly define the range using those what will the valid range of the element of type integer be? So if I have an element as defined <xs:element name="MyIntegerElement" type="xs:integer"/> and I create an XML file with <MyIntegerElement>1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</MyIntegerElement> Will that validate against the XSD?Zitella
That's a perfectly valid integer (xs:integer), so if you validate it against the schema it will say that it is ok. Now, if you want it to fit, in say, 32 bits, you might want to use xs:int, in which case the schema will say that that particular value is too big to fit. It all boils down to what exactly are you trying to represent with your data. The xs:integer VS xs:int is a nice starting point to understand the differences between the representation of the data. There is a nice post here: #15337372Winterize
Thanks. I think you answered my question there. My intention was to have these data types bounded by default relevant to the platform they are used on but it seems in this case int was what I wanted to use rather than integer which appears to be boundless. I could still use integer but I would have to explicitly define the limits but then it would still not necessarily be cross platform.Zitella
My pleasure. On a last note, if you do have sensible ranges that make sense for your application, by all means, set the min/max values even if you are using a bounded data type. Also, there is a nice set of built-in datatypes (w3.org/TR/xmlschema-2/#built-in-datatypes) ex: negativeInteger, unsignedInt, etc, be sure to explore these before taking the trouble to define your own :)Winterize
And this diagram w3.org/TR/xmlschema-2/#built-in-datatypes just helped me understand this a whole lot more. The base type is decimal which defines how many digits you can have, and then integer is derived from decimal which eliminates the fractional numbers from the value, then long is derived from integer which sets a default min and max value range, and then int is derived from long which redefines the min and max to a smaller value range.Zitella
Yep, you got it perfectly.Winterize

© 2022 - 2024 — McMap. All rights reserved.