thegunner
thegunner

Reputation: 7153

XML integer element with leading zeros

I have an XML file with an element that is of integer type but it has leading zeros. Before I send the XML off I can see in the xml file that the integer is e.g. 001234 ...but when getting processed as it's meant to be an integer the leading zeros are removed.

Is there a way in the xml or xsd to specify that this integer is always 6 characters in length? (Before I tell at the people who are receiving the xml to change type to convert at their end)

Thanks,

Upvotes: 1

Views: 7679

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

Declaring the type as xs:integer is telling everyone that leading zeros are not significant, i.e. that 0012 and 12 are interchangeable and equivalent, because that is part of what it means to be an integer. You can use a pattern facet to restrict the length to 6 digits, but you will get yourself into a mess, because for example if you do an identity transform through a schema-aware processor then the leading zeros are likely to be lost.

If leading zeroes really are significant (as in a phone number, for example), then it shouldn't be typed as an integer, but as a string matching the pattern \d+.

Upvotes: 8

forty-two
forty-two

Reputation: 12817

Yes, you should be able to use the decimal type in your schema.

Upvotes: -3

Related Questions