I'm new to XML and wanted to know what will the restriction's base be for these attributes in a XSD file? string, positiveInteger, or decimal?
<xs:attribute name="sku" use="required">
<xs:simpleType>
<xs:restriction base="xs:positiveInteger">
<xs:pattern value="\d{5}"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="price" use="required">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:pattern value="\d \.\d{2}"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
CodePudding user response:
Personally, I tend to be suspicious of attempts to restrict numeric types using regular expressions.
For example, the pattern \d{5}
suggests that 01234 is legal but 1234 is not. That has some practical problems because many applications outputting an integer are likely to prefer to format it as 1234. It also suggests that 01234 and 1234 are not equivalent, which makes me question whether this is really an integer, or just a string consisting of digits. Does it make sense to add two of these values? If not, I would question whether modelling it as an integer is correct.
(A classic example here is phone numbers, where leading zeros are significant, and addition makes no sense. Modelling phone "numbers" as integers is therefore wrong.)
Similarly, I can see why you might want to require a value to be expressed as 8.10 rather than 8.1, because that's the way money amounts are usually written. But it feels wrong to me: if it's a decimal value, then 8.10 and 8.1 are different ways of writing the same number.