3

I am parsing following XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
  <xs:simpleType name="Money">
    <xs:restriction base="xs:decimal">
      <xs:pattern value="\d+\.\d{2}" /> 
      <xs:minInclusive value="0.00" /> 
      <xs:maxInclusive value="9999999.99" />
    </xs:restriction>
  </xs:simpleType>

 
  <xs:element name="Document" id="Document">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Issue" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="ChargeMinimum" use="optional" type="Money" default="0.00" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I'm getting an error:

a-props-correct.2: Invalid value constraint value '0.00' in attribute 'ChargeMinimum'. cvc-pattern-valid: Value '0.0' is not facet-valid with respect to pattern '\\d+.\\d{2}' for type 'Money'.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
A G
  • 31
  • 2
  • Do you get that error when just parsing/validating the schema? Or with a certain instance XML (containing e.g. ``, perhaps)? – Martin Honnen Mar 20 '23 at 10:34
  • Interesting, this seems to be a (Java?) Xerces error which seems to check the representation of the ("normalized"?) minimum value `0.0` against the pattern. Seems like a quirk or bug in Xerces, I don't get any error of that sort with Saxon EE or .NET's built-in validator. Might be worth asking on the xml-developers mailing list or just to wait until the experts on schemas or some schema validator implementors show up here. – Martin Honnen Mar 20 '23 at 10:46

2 Answers2

1

What seems to happen here is that Xerces applies rule 2 from https://www.w3.org/TR/xmlschema-1/#coss-attribute which says:

2 if there is a {value constraint}, the canonical lexical representation of its value must be ·valid· with respect to the {type definition} as defined in String Valid (§3.14.4).

So value constraint is the default="0.00", the canonical lexical representation of the decimal 0.00 is (as far as I understand it) 0.0 and that value is failing validation against the pattern <xs:pattern value="\d+\.\d{2}" />.

So based on my current reading of the spec Xerces is giving an appropriate error message.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

I was able to reproduce using Xerces-J, and I concur with @MartinHonnen's assessment (+1).

As a work-around, consider using xs:fractionDigits instead of xs:pattern to limit the decimal places:

<xs:restriction base="xs:decimal">
  <xs:fractionDigits value="2"/>
  <xs:minInclusive value="0.00" /> 
  <xs:maxInclusive value="9999999.99" />
</xs:restriction>

Thanks to @MartinHonnen for pointing out a flaw in my previously suggested work-around.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • @MartinHonnen: You're right. I had confirmed the original issue but not my workaround. It's not the `xs:minInclusive="0.00"` value that's the problem -- it's the `default="0.00"`. – kjhughes Mar 20 '23 at 13:09
  • 1
    @MartinHonnen: `xs:fractionDigits` would be a much better work-around. – kjhughes Mar 20 '23 at 13:42