xsd pattern acceptable for decimals?

Sebastiaan van den Broek picture Sebastiaan van den Broek · Feb 23, 2012 · Viewed 13k times · Source

We have a request to implement our webservice response so that xsd:decimal fraction digits will be zero-padded when it's not long enough when a pattern indicates so. I am wondering if this is a reasonable request and if xsd:decimal is supposed to be used with patterns like these. Here is the relevant part of the xsd according to their specs:

<xsd:simpleType>
   <xsd:restriction base="xsd:decimal">
       <xsd:totalDigits value="14"/>
       <xsd:fractionDigits value="2"/>
       <xsd:pattern value="[\-+]?[0-9]{1,12}[.][0-9]{2}"/>
   </xsd:restriction>
</xsd:simpleType>

So the fractionDigits are set to 2 which means the precision can be a maximum of 2 digits. According to http://zvon.org/xxl/XMLSchemaTutorial/Output/ser_types_st2.html it is also fine if there are less fraction digits (for example for a number like 5.1)

But according to the pattern {2} there should always be 2 fraction digits.

We're developing a generic application development platform (Mendix) and there's no telling what the decimal will be used for in advance (currency, pH values, distances, etc). This case comes from a specific project where our platform is being used but normally we won't know what kind of data is being transferred. We could decide to just follow the WSDL in this regard which states it should have 2 fraction digits. but our implementation of it must be very generic.

There is nothing stating with what exactly these fraction digits should be padded with or even that we should pad instead of just leaving out this decimal altogether. In theory we could decide to pad with 5's until it matches the pattern. As far as I know patterns are rarely used and if they are it's used for things like passwords. The XSD specification is vague though so it would be appreciated if someone could shed some light on whether this is valid use of an XSD and if it makes sense for us to decide to pad with 0's.

Answer

Petru Gardea picture Petru Gardea · Feb 23, 2012

To be practical, my litmus test would be to look at this from the perspective of the technology stack directly involved or, better yet, that is mainstream.

I can think of JAXB on Java or xsd/svcutil/etc.exe on .NET; A quick test of these pretty common tools against this schema fragment using a value of 1 would fail to produce valid XML. This would send developers scrambling for all sorts of customizations to make it work as per the XSD pattern. Painfull, high developing and maintenance costs...

The same would be applicable to an XSLT; there will be a need to manually format the output... Bottom line, XSD patterns are not machine usable for "automatic" formatting... I have yet to see such a thing...

I also believe that a requirement such as this is unreasonable and I personally feel that it should be considered as an antipattern when it comes to describing data being exchanged. Since there's no absolute, it is conceivable that there must be an exception; I can't think of any, but one must explore the reason why you were presented with such a requirement; I would then try to find a solution that wouldn't involve this pattern...