4

I am getting error validating with this XML:

XSD

<xs:simpleType name="XYZ">
    <xs:restriction base="xs:nonNegativeInteger">
      <xs:enumeration value="1">
      </xs:enumeration>
      <xs:enumeration value="2">
      </xs:enumeration>
    </xs:restriction>
  </xs:simpleType>

XML value :

 <XYZ>2</XYZ>

Error

cvc-enumeration-valid: Value '2' is not facet-valid with respect to enumeration '[1]'. It must be a value from the enumeration.

Can anyone please help me to understand the problem? How to resolve it ?

Ramesh
  • 340
  • 1
  • 7
  • 21
  • 1
    I could not reproduce the error. It would be useful to have the entire schema as well as the instance, or any minimal example where the error appears so we can help you. – Ghislain Fourny Aug 21 '17 at 12:25

2 Answers2

4

The error message,

cvc-enumeration-valid: Value '2' is not facet-valid with respect to enumeration '[1]'. It must be a value from the enumeration.

and the simpleType from your question do not agree.

The error message implies that only 1 is allowed yet 2 was encountered; your type definition does indeed allow both 1 and 2.

To elicit an actual error message pertaining to your xs:simpleType, your XML would have to use a value, say 3, not allowed. Then, you would receive an error message like this:

cvc-enumeration-valid: Value '3' is not facet-valid with respect to enumeration '[1, 2]'. It must be a value from the enumeration.

Therefore, your (first, maybe only?) mistake is in believing that the posted xs:simpleType definition has anything to do with that error message.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Hi kjhughes, Thanks for writing. is it problem with simpleType ? why validation failing when my xml value is 2 ? – Ramesh Aug 21 '17 at 13:07
  • Re-read my answer. Your validation would not fail with the posted `simpleType` for an XML value of `2`. The error message you posted simply does not correspond with such an outcome. If this fact does not sufficiently help you to resolve your problem, add to your question a ***[mcve]*** (small XML file and small XSD and exact error message) and we'll help you further. – kjhughes Aug 21 '17 at 13:20
1

I've got this working, I think it addresses your question, but as KJ indicates without an example we're really just guessing.

Here's a sample XML

<xml>
    <XYZ>3</XYZ>
</xml>

And a sample schema

<xs:schema 
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="xml">
    <xs:complexType>
        <xs:sequence>
            <xs:element name='XYZ'>
              <xs:simpleType>
                <xs:restriction base="xs:nonNegativeInteger">
                  <xs:enumeration value="1"/>
                  <xs:enumeration value="2"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
 </xs:element>
</xs:schema>

With a value of 3 (invalid), I get the following exception.

The 'XYZ' element is invalid - The value '3' is invalid according to its datatyp
e 'NonNegativeInteger' - The Enumeration constraint failed. Line: 2 Column: 10
William Walseth
  • 2,803
  • 1
  • 23
  • 25