"IDREF" Datatype Values and Representations

This section describes the built-in datatype 'IDREF' designed to primarily support the id reference attribute to reference to a specific XML element. 'IDREF' values must be 'NCName' values and must match existing 'ID' values.

The third datatype derived from "NCName" is "IDREF, which is designed to primarily support values used in the id reference attribute to refer to another specific XML element.

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

Here is a sample XSD document that defines 2 sub element <IDREFElement> and <IDREFAttribute> to use "IDREF" values as element contents and as element attributes:

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- IDREF_datatype_test.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:element name="IDREF_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:element name="IDREF_Element" type="xs:IDREF" 
        maxOccurs="unbounded"/>
      <xs:element name="IDREF_Attribute" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="IDREF" type="xs:IDREF"/>
        </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"?>
<!-- IDREF_datatype_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<IDREF_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 valid "ID" values as attribute values -->
  <ID_Attribute ID=" top.left   "/>
  <ID_Attribute ID="    First_Signature  "/>
  <ID_Attribute ID=" Item123   "/>

<!-- 3 valid "IDREF" values as element contents -->
  <IDREF_Element>    site-logo </IDREF_Element>
  <IDREF_Element>    site-logo </IDREF_Element>
  <IDREF_Element>  First_Signature</IDREF_Element>

<!-- 2 invalid "IDREF" values as element contents -->
  <IDREF_Element>    not_defined </IDREF_Element>
  <IDREF_Element>  not a valid NCName </IDREF_Element>

<!-- 3 valid "IDREF" values as attribute values -->
  <IDREF_Attribute IDREF="   site-logo   "/>
  <IDREF_Attribute IDREF=" First_Signature   "/>
  <IDREF_Attribute IDREF="    stdin   "/>

<!-- 1 invalid "IDREF" values as attribute values -->
  <IDREF_Attribute IDREF="   unkown  "/>
</IDREF_Datatype_Test>

Here is the output of the JDK 1.7 tool XsdSchemaValidator.java on this test. It only reports errors for 2 invalid "IDREF" values:

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

   Error:
   Line number: 24
   Column number: 53
   Message: cvc-datatype-valid.1.2.1: 'not a valid NCName' is not a 
   valid value for 'NCName'.

Error:
   Line number: 24
   Column number: 53
   Message: cvc-type.3.1.3: The value '  not a valid NCName ' of 
   element 'IDREF_Element' is not valid.

Error:
   Line number: 32
   Column number: 21
   Message: cvc-id.1: There is no ID/IDREF binding for IDREF 
   'not_defined'.

Failed with errors: 3

It looks like XsdSchemaValidator.java can only report the first occurrence of undefined "ID" values used as "IDREF" value.

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