"ID" Datatype Values and Representations

This section describes the built-in datatype 'ID' designed to primarily support the id attribute to identify a specific XML element. 'ID' values must be 'NCName' values and must be unique in a single XML document.

The second datatype derived from "NCName" is "ID, which is designed to primarily support values used in the id attribute to identify a specific XML element.

"ID" is a datatype derived from "NCName" datatype with these rules:

Here is a sample XSD document that defines 2 sub element <IDElement> and <IDAttribute> to use "ID" values as element contents and as element attributes:

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

Here is a sample XML document to test these definitions:

<?xml version="1.1"?>
<!-- id_datatype_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<ID_Datatype_Test>
<!-- 3 valid "ID" values as element contents -->
  <ID_Element>    site-logo </ID_Element>
  <ID_Element>  cover.photo </ID_Element>
  <ID_Element>  stdin       </ID_Element>

<!-- 3 invalid "ID" values as element contents -->
  <ID_Element>site-logo </ID_Element>
  <ID_Element>  2835c0d60046c109    </ID_Element>
  <ID_Element>  std:out     </ID_Element>

<!-- 3 valid "ID" values as attribute values -->
  <ID_Attribute ID=" top.left   "/>
  <ID_Attribute ID="    First_Signature  "/>
  <ID_Attribute ID=" Item123   "/>

<!-- 1 invalid "ID" values as attribute values -->
  <ID_Attribute ID=" stdin   "/>
</ID_Datatype_Test>

Here is the output of the JDK 1.7 tool XsdSchemaValidator.java on this test. It is reporing 4 groups of errors for those 4 invalid elements.

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

Error:
   Line number: 13
   Column number: 36
   Message: cvc-id.2: There are multiple occurrences of ID value 
   'site-logo'.

Error:
   Line number: 13
   Column number: 36
   Message: cvc-type.3.1.3: The value 'site-logo ' of element 
   'ID_Element' is not valid.

Error:
   Line number: 14
   Column number: 48
   Message: cvc-datatype-valid.1.2.1: '2835c0d60046c109' is not a 
   valid value for 'NCName'.

Error:
   Line number: 14
   Column number: 48
   Message: cvc-type.3.1.3: The value '  2835c0d60046c109    ' of 
   element 'ID_Element' is not valid.

Error:
   Line number: 15
   Column number: 40
   Message: cvc-datatype-valid.1.2.1: 'std:out' is not a valid value
   for 'NCName'.

Error:
   Line number: 15
   Column number: 40
   Message: cvc-type.3.1.3: The value '  std:out     ' of element 
   'ID_Element' is not valid.

Error:
   Line number: 23
   Column number: 32
   Message: cvc-id.2: There are multiple occurrences of ID value 
   'stdin'.

Error:
   Line number: 23
   Column number: 32
   Message: cvc-attribute.3: The value ' stdin   ' of attribute 
   'ID' on element 'ID_Attribute' is not valid with respect to its 
   type, 'ID'.

Failed with errors: 8

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