简体   繁体   中英

Xml validation of empty tag

I have xml File with element date type:

...
<startDate />
...

in xsd file this type is described:

<xs:element name="startDate " type="xs:date" nillable="true" />

When I validate xml with SchemaValidator I have an exception

org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '' является недопустимым значением для 'date'.

when tag startDate is not empty all is ok. But when it is empty exceptioin occur. I can only change xsd-schema file, but not xml, because I receive it from another system.

Since you cannot change the XML document, you could try to build a union type of xs:date with an empty string:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="empty-string">
    <xs:restriction base="xs:string">
      <xs:maxLength value="0"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="startDate">
    <xs:simpleType>
      <xs:union memberTypes="xs:date empty-string"/>
    </xs:simpleType>
  </xs:element>
</xs:schema>

against which

<startDate/>

should validate.

Note: If you could change your XML document, this would probably validate successfully against your original schema (because of the nillable attribute):

<startDate
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:nil="true"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM