Search code examples
xmltypesxsdschemafractions

XSD cutom type for fraction numbers with / symbol


Hi fellow programmers,

I am trying to create custom XSD type extension to accept double or two doubles with "/" symbol in the middle. This way I want to express fraction numbers. For example:

<some_element>12.34</some_element>
<some_element>1/23</some_element>
<some_element>12.34/56.78</some_element>
<some_element>12.34/56.78</some_element>
<some_element>-1/2</some_element>

must be valid.

Any ideas?

Many thanks


Solution

  • You'll have to define it as a restriction of xs:string using a pattern facet. The pattern for xs:double is given in the XSD 1.1 spec as

    (\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)([Ee](\+|-)?[0-9]+)? |(\+|-)?INF|NaN
    

    and from that it's trivial to allow one of these, or two separated by "/"