"language" Datatype Values - Invalid Inputs

This section provides a tutorial example on invalid input XML elements for 'language' datatype. JDK 1.7 is the best tool reporting validation errors.

When an XML element is defined to use "language" datatype, element content may fail to pass the validation, if it does not represent a valid "language" value.

Let's continue to use the XSD document, language_datatype_test.xsd, in the previous tutorial to test the following sample XML document, which contains some invalid <Language> elements:

<?xml version="1.1"?>
<!-- language_datatype_invalid_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<Language_Datatype_Test>
<!-- 3 invalid "Language" elements -->
  <Language>     en_US</Language>
  <Language>US English</Language>
  <Language>       C++</Language>
</Language_Datatype_Test>

Test #1 with XMLPad 3.0.2.1: Open language_datatype_invalid_test.xml with XMLPad; Assign XSD schema file language_datatype_test.xsd; Perform validation. You see 3 errors with the same error message: "Datatype check error for element Language{}: value is invalid for datatype 'language'."

XMLPad 3.0.2.1 does report invalid "language" elements. But its error message is useless.

Test #2 with Notepad++ 5.9 with "XML Tools Plugin": Open language_datatype_invalid_test.xml with XMLPad; Perform validation with XSD schema file language_datatype_test.xsd provided. You see 3 errors with much better error messages:

Validation of current file using XML schema:

ERROR: Element 'Language': 
  '     en_US' is not a valid value of the atomic type 'xs:language'.
ERROR: Element 'Language': 
  'US English' is not a valid value of the atomic type 'xs:language'.
ERROR: Element 'Language': 
  '       C++' is not a valid value of the atomic type 'xs:language'.

Notepad++ 5.9 with "XML Tools Plugin" also reports invalid "language" elements. Its error messages are much better.

Test #3 with JDK 1.7: Compile and run XsdSchemaValidator.java You see 3 groups of errors with much more detailed information:

c:\Progra~1\Java\jdk1.7.0_07\bin\java.exe XsdSchemaValidator 
   language_datatype_test.xsd language_datatype_invalid_test.xml

Error:
   Line number: 8
   Column number: 34
   Message: cvc-pattern-valid: Value 'en_US' is not facet-valid with 
   respect to pattern '([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*' for type
   'language'.

Error:
   Line number: 8
   Column number: 34
   Message: cvc-type.3.1.3: The value 'en_US' of element 'Language' 
   is not valid.

Error:
   Line number: 9
   Column number: 34
   Message: cvc-pattern-valid: Value 'US English' is not facet-valid 
   with respect to pattern '([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*' for 
   type 'language'.

Error:
   Line number: 9
   Column number: 34
   Message: cvc-type.3.1.3: The value 'US English' of element 
   'Language' is not valid.

Error:
   Line number: 10
   Column number: 34
   Message: cvc-pattern-valid: Value 'C++' is not facet-valid with 
   respect to pattern '([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*' for type
   'language'.

Error:
   Line number: 10
   Column number: 34
   Message: cvc-type.3.1.3: The value 'C++' of element 'Language' is
   not valid.

Failed with errors: 6

I like the JDK 1.7 tool better, because it gives more details on why the XML element is not valid. Notice that JDK 1.7 reports errors with input values already processed into "token" values. For example, 'C++' is reported in the error, not the original input value, ' C++'.

Last update: 2013.

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

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

"string" and Its Derived Datatypes

 "string" Datatype Values and Representations

 "normalizedString" Datatype Values and Representations

 "token" Datatype Values and Representations

 "language" Datatype Values and Representations

"language" Datatype Values - Invalid Inputs

 "Name" Datatype Values and Representations

 "NMTOKEN" Datatype Values and Representations

 "NCName" Datatype Values and Representations

 "ENTITY" Datatype Values and Representations

 "ID" Datatype Values and Representations

 "IDREF" Datatype Values and Representations

 "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