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:
A "IDREF" value must be "NCName" value.
A "IDREF" value must be referring the existing "ID" value exist in same XML document.
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:
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.