0

I am trying to implement basic validation on XML against a defined XSD XML:

<Employee type="permanent">
    <Name>John</Name>
    <employeeId>9000</employeeId>
    <Age>28</Age>
    <dateOfBirth>28/12/2000</dateOfBirth>
    <city>Vancouver</city>
    <salary>120000</salary>
</Employee>

XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Employee">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string"></xs:element>
                <xs:element name="employeeId" type="xs:int"></xs:element>
                <xs:element name="Age" type="xs:int"></xs:element>
                <xs:element name="dateOfBirth" type="xs:date"></xs:element>
                <xs:element name="city" type="xs:string"></xs:element>
                <xs:element name="salary" type="xs:int"></xs:element>
            </xs:sequence>
            <xs:attribute name="type" type="xs:string"></xs:attribute>
        </xs:complexType>
    </xs:element>
</xs:schema>

But when I run the validator:

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(schemaPath));
            Validator validator = schema.newValidator();
            validator.setErrorHandler(new BasicErrorHandler());
            validator.validate(new StreamSource(new File(xmlPath)));

I keep getting the following error:

cvc-datatype-valid.1.2.1: '12/28/2000' is not a valid value for 'date'.
cvc-type.3.1.3: The value '12/28/2000' of element 'dateOfBirth' is not valid.

Now when I tried searching for a solution on SO and Google here is what I found:

<xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="testdate" type="zsdate"/>
                <xs:element name="testtime" type="zstime"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="zsdate">
        <xs:restriction base="xs:date">
            <xs:pattern value="^(0[1-9]|[12][0-9]|3[01]).(0[1-9]|1[012]).(19|20)\d\d$"/>
        </xs:restriction>
    </xs:simpleType>

I do not want to convert the data type to string and validate against a regex. My date will always be in dd/mm/yyyy format. The way it is written in XML pasted above.

Is there something that I am missing.

P.S. when I use the following:

   <dateOfBirth>2012-08-22</dateOfBirth>

it works fine.

john.p.doe
  • 411
  • 2
  • 10
  • 21

1 Answers1

2

<xs:date> must be in YYYY-MM-DD format. So either you write it that way or you take <xs:string> with the regex.

halloei
  • 1,892
  • 1
  • 25
  • 45
  • I was afraid of that, isnt there any way i can specify a format in XSD – john.p.doe Feb 02 '16 at 09:28
  • I'm afraid not. What's the problem with ``? – halloei Feb 02 '16 at 11:09
  • 3
    A bit of background here: there are some who take a rather firm view that XML is designed for data interchange and when you do data interchange you should use international standards for formats such as dates, not local conventions that might apply in parts of your user community. If you want users to see dates in d/m/y or m/d/y formats, that should be part of the presentation layer of the application, it shouldn't influence the data interchange and storage layer. – Michael Kay Feb 02 '16 at 15:24
  • @halloei I finally gave up and tried the string method and it does not work either. I dont want to update the question because it will become super long for a reader. I will post another question and paste the link here. – john.p.doe Feb 02 '16 at 15:51
  • @MichaelKay Thanks Michael, what you said makes a lot of sense. – john.p.doe Feb 02 '16 at 15:52
  • @john.p.doe [Have a look at this answer](http://stackoverflow.com/a/1048049/3318377) – halloei Feb 02 '16 at 15:55