RE: Defining uniqueness on optional element

> There's no way to write identity constraints which make this OK but
> rule out the obvious bad cases.  The problem is not that it's an
> _error_ for a field of a unique constraint to be missing, it's just
> that if a field _is_ missing then the selected element is not
> considered at all.  So in the above example, a two-part unique on
> 'root' which selects 'mayHaveDaughter' with fields '@f1' and 'd' will
> not constrain empty 'mayHaveDaughter' elements at all.

I have tested the following:

xsd file:

<?xml version="1.0"?>

<xsd:schema xmlns:ipm="http://unique/test"
			xmlns:xsd="http://www.w3.org/2001/XMLSchema"
			targetNamespace="http://unique/test"
			elementFormDefault="qualified">
			
<xsd:element name="root">
	<xsd:complexType>
		<xsd:sequence>
			<xsd:element ref="ipm:mayHaveDaughter" minOccurs="0"
maxOccurs="unbounded"/>
		</xsd:sequence>
	</xsd:complexType>
	<xsd:unique name="uniqueNode">
		<xsd:selector xpath="ipm:mayHaveDaughter"/>
		<xsd:field xpath="@f1"/>
		<xsd:field xpath="ipm:d"/>
	</xsd:unique>
</xsd:element>

<xsd:element name="mayHaveDaughter">
	<xsd:complexType>
		<xsd:sequence>
			<xsd:element name="d" minOccurs="0" maxOccurs="1"
type="xsd:string"/>
		</xsd:sequence>
		<xsd:attribute name="f1" type="xsd:string"/>
	</xsd:complexType>
</xsd:element>

</xsd:schema>

example file 1:
<?xml version="1.0"?>

<root xmlns="http://unique/test" 
	  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xsi:schemaLocation="http://unique/test unique.xsd">
	  
 <mayHaveDaughter f1="a"/>
 <mayHaveDaughter f1="b"/>
 <mayHaveDaughter f1="c"/>
 <mayHaveDaughter f1="c"/>
</root>

Both MSXML4.0 and xsdvalid give an error that indicate 'c' as duplicate key.
So if indeed all the <mayHaveDaughter> elements that have no <d> element are
ignored, no error should be generated in this case. Or am I wrong with that
? In case of Xerces I get an error message: '[Error] unique.xml:7:27: Not
enough values specified for <unique> identity constraint specified for
element "root" ' In neither cases this is a valid error. 
I have tried some other combinations and to me it seems like the parsers
behave as following:
	* When one of the fields is not available there is still be checked
on the other fields

I have tried to find this back in the recs, but I didn't succeed in it.

Am I missing something here ?

Ismaël

Received on Thursday, 27 March 2003 07:34:31 UTC