"NMTOKEN" Datatype Values and Representations

This section describes the built-in datatype 'NMTOKEN' designed to support XML 'NMTOKEN' attribute values. 'NMTOKEN' datatype is derived from 'token' by restricting the value set according the XML 'NMTOKEN' specification.

The third datatype derived from "token" is "NMTOKEN", which is designed to support "NMTOKEN" attribute values defined in the XML 1.1 specification.

"NMTOKEN" (stands for Name Token) is a datatype derived from "token" datatype by limiting values to those satisfy the following pattern defined in XML 1.1:

NameStartChar::= ([:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF]|
                  [\u0370-\u037D\u037F-\u1FFF\u200C-\u200D]|
                  [\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF]|
                  [\uF900-\uFDCF\uFDF0-\uFFFD\u010000-\u0EFFFF]
NameChar     ::= (NameStartChar|[-.0-9\xB7\u0300-\u036F\u203F-\u2040])
NMTOKEN      ::= (NameChar)+

If you comparing this pattern with the "Name" datatype pattern, you will see that "NMTOKEN" value set is slightly bigger than "Name" value set. "NMTOKEN" values accept [-.0-9\xB7\u0300-\u036F\u203F-\u2040] as the first non white-space character.

Here is a sample XSD document that defines a sub element <NMTOKEN> to use "NMTOKEN" values:

<?xml version="1.1"?>
<!-- NMTOKEN_datatype_test.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="NMTOKEN_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="NMTOKEN" type="xs:NMTOKEN" 
        maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

Here is a sample XML document that contains <NMTOKEN> elements to test that definition:

<?xml version="1.1"?>
<!-- NMTOKEN_datatype_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<NMTOKEN_Datatype_Test>
<!-- 6 valid "NMTOKEN" elements -->
  <NMTOKEN>NMTOKEN_datatype_test</NMTOKEN>
  <NMTOKEN>     html  </NMTOKEN>
  <NMTOKEN>   xs:token</NMTOKEN>
  <NMTOKEN>    .htaccess </NMTOKEN>
  <NMTOKEN>   &#x5934;</NMTOKEN>
  <NMTOKEN>   2nd_place</NMTOKEN>

<!-- 4 invalid "NMTOKEN" elements -->
  <NMTOKEN>complex Type</NMTOKEN>
  <NMTOKEN>      (html)</NMTOKEN>
  <NMTOKEN>    xs+token</NMTOKEN>
  <NMTOKEN>    &#x3000;</NMTOKEN>
</NMTOKEN_Datatype_Test>

Run the JDK 1.7 tool XsdSchemaValidator.java again, you will see 4 groups of errors for the last 4 invalid elements. But error messages do not provide the "NMTOKEN" string pattern

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

...

Error:
   Line number: 17
   Column number: 34
   Message: cvc-datatype-valid.1.2.1: 'xs+token' is not a valid value
   for 'NMTOKEN'.

Error:
   Line number: 17
   Column number: 34
   Message: cvc-type.3.1.3: The value 'xs+token' of element 'NMTOKEN'
   is not valid.

Error:
   Line number: 18
   Column number: 34
   Message: cvc-datatype-valid.1.2.1: '?' is not a valid value for 
   'NMTOKEN'.

Error:
   Line number: 18
   Column number: 34
   Message: cvc-type.3.1.3: The value '?' of element 'NMTOKEN' is not
   valid.

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