XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

Examples of XSD 1.1 and XML Files with Errors

This section provides an example XML file associated with an example XSD 1.1 file. The XML file contains some errors intentionally for see how XML validation tools report them.

Now I have a tool to validate XML documents against XSD 1.1 schema. I created a XSD 1.1 schema file, xsd11_datatype_test.xsd, with more new datatypes introduced in XSD 1.1.

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- xsd11_datatype_test.xsd
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<xs:element name="XSD11_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="AnyAtomicType" type="xs:anyAtomicType" 
        maxOccurs="unbounded"/>
      <xs:element name="DateTimeStamp" type="xs:dateTimeStamp" 
        maxOccurs="unbounded"/>
      <xs:element name="DayTimeDuration" type="xs:dayTimeDuration" 
        maxOccurs="unbounded"/>
      <xs:element name="YearMonthDuration" type="xs:yearMonthDuration" 
        maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

I also created a sample XML document, xsd11_datatype_test.xml, with some incorrect elements:

<?xml version="1.1"?>
<!-- xsd11_datatype_test.xml
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->

<XSD11_Datatype_Test 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:noNamespaceSchemaLocation="xsd11_datatype_test.xsd">

<!-- 3 valid "anyAtomicType" representations -->
  <AnyAtomicType>   1971-05-16T00:00:01  </AnyAtomicType>
  <AnyAtomicType>   &#x5934;             </AnyAtomicType>
  <AnyAtomicType><![CDATA[   Hello <b>Herong</b>!   ]]>
  </AnyAtomicType>
 
<!-- 1 invalid "anyAtomicType" representations -->
  <AnyAtomicType>   Hello <b size="9">Herong</b>! </AnyAtomicType>

<!-- 1 valid "dateTimeStamp" representations -->
  <DateTimeStamp>   1971-05-16T00:00:01Z   </DateTimeStamp>

<!-- 1 invalid "dateTimeStamp" representations -->
  <DateTimeStamp>   &#x5934;      </DateTimeStamp>

<!-- 1 valid "dayTimeDuration" representations -->
  <DayTimeDuration>    P3DT5H20M30.123S  </DayTimeDuration>

<!-- 1 invalid "dayTimeDuration" representations -->
  <DayTimeDuration>   P1Y2M3DT5H20M30.123S    </DayTimeDuration>

<!-- 1 valid "yearMonthDuration" representations -->
  <YearMonthDuration>    P1Y2M  </YearMonthDuration>

<!-- 1 invalid "yearMonthDuration" representations -->
  <YearMonthDuration>   P1Y2M3DT5H20M30.123S    </YearMonthDuration>
</XSD11_Datatype_Test>

When I run jaxp.SourceValidator on xsd11_datatype_test.xml, I get the following output:

>jdk8x2r jaxp.SourceValidator -xsd11 -i xsd11_datatype_test.xml

[Error] xsd11_datatype_test.xml:17:67: cvc-type.3.1.2: Element 
^^^ 'AnyAtomicType' is a simple type, so it must have no element 
^^^ information item [children].
[Error] xsd11_datatype_test.xml:23:51: cvc-datatype-valid.1.2.1: '?' 
^^^ is not a valid value for 'dateTimeStamp'.
[Error] xsd11_datatype_test.xml:23:51: cvc-type.3.1.3: The value 
^^^ '   ?      ' of element 'DateTimeStamp' is not valid.
[Error] xsd11_datatype_test.xml:29:65: cvc-datatype-valid.1.2.1: 
^^^ 'P1Y2M3DT5H20M30.123S' is not a valid value for 'dayTimeDuration'.
[Error] xsd11_datatype_test.xml:29:65: cvc-type.3.1.3: The value 
^^^ '   P1Y2M3DT5H20M30.123S    ' of element 'DayTimeDuration' is not 
^^^ valid.
[Error] xsd11_datatype_test.xml:35:69: cvc-datatype-valid.1.2.1: 
^^^ 'P1Y2M3DT5H20M30.123S' is not a valid value for 
^^^ 'yearMonthDuration'.
[Error] xsd11_datatype_test.xml:35:69: cvc-type.3.1.3: The value 
^^^ '   P1Y2M3DT5H20M30.123S    ' of element 'YearMonthDuration' is 
^^^ not valid.
xsd11_datatype_test.xml: 96 ms

Good. jaxp.SourceValidator reported all errors correctly with line numbers and column numbers indicate where errors occurred in the XML source document.

Last update: 2014.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

Xerces2 Java Parser - Java API of XML Parsers

 Installing Xerces2 Java Parser for XSD 1.1

 Verifying Installation of Xerces2

 Xerces2 Sample Program List

 Xerces2 Sample Program dom.Counter

 dom.Counter Validating XML with Associated XSD

 dom.GetElementsByTagName and dom.Writer

 sax.DocumentTracer and sax.Writer

 Examples of XSD and XML Files with Errors

 sax.Writer Reporting Errors Embedded in XML Structure

 XSD 1.1 not Supported by sax.Writer

 XSD 1.1 Supported by jaxp.SourceValidator

Examples of XSD 1.1 and XML Files with Errors

 jaxp.TypeInfoWriter as an XSD 1.1 Validation Tool

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Examples of XSD 1.1 and XML Files with Errors - Updated in 2014, by Dr. Herong Yang