Constraints, keys, and validation

Hello,

I have a problem with some unicity constraints : I read in
Eric Van der Vlist's book, that you can use constraints to
check that an element has either an attribute, or an elment,
but bot both of them. I wanted to try to validate the fact
that it can have either an attribute a1, or an attribute a2,
but not both.

I made a little exemple, which follows : 

-------------- the schema ----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified"
	targetNamespace="http://www.xmlmind.com/xmleditor/schema/exemple/"
	xmlns="http://www.xmlmind.com/xmleditor/schema/exemple/"
	xmlns:ex="http://www.xmlmind.com/xmleditor/schema/exemple/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">

	<xsd:complexType name="Type">
		<xsd:sequence>
			<xsd:element name="elt" type="xsd:string" minOccurs="0"
maxOccurs="1"/>
		</xsd:sequence>
		<xsd:attribute name="foo" type="xsd:string"/>
		<xsd:attribute name="bar" type="xsd:string"/>
	</xsd:complexType>
	
	<xsd:complexType name="Test">
		<xsd:sequence>
			
			<xsd:element name="mustHaveFooOrElt" type="Type"
minOccurs="4" maxOccurs="4">
				<xsd:key name="eltOrfoo">
					<xsd:selector xpath="."/>
					<xsd:field xpath="@foo|ex:elt"/>
				</xsd:key>
			</xsd:element>
			
			<xsd:element name="mustHaveFooOrBar" type="Type"
maxOccurs="4" minOccurs="4">
				<xsd:key name="fooOrBar">
					<xsd:selector xpath="."/>
					<xsd:field xpath="@foo|@bar"/>
				</xsd:key>
			</xsd:element>
		</xsd:sequence>
	</xsd:complexType>
	
	<xsd:element name="test" type="Test"/>
	
</xsd:schema>
----------------------------- the XML instance file 
--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<test
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.xmlmind.com/xmleditor/schema/exemple/"
   
xsi:schemaLocation="http://www.xmlmind.com/xmleditor/schema/exemple/
test.xsd">
    
        <!--1 : Should be valid : the key is associated with a
single value eg: 'foo' -->
	<mustHaveFooOrElt foo="foo"/>
    
        <!-- 2 : Should be valid : the key is associated with
a single value eg: 'element' -->
        <mustHaveFooOrElt>
    	    <elt>element</elt>
        </mustHaveFooOrElt>

        <!-- 3 : Should be invalid : the key is associated
with 2 values -->
	<mustHaveFooOrElt foo="foo">
		<elt>element</elt>
	</mustHaveFooOrElt>    
	
        <!-- 4 : Should be invalid : the key is associated
with no value.-->
	<mustHaveFooOrElt bar="bar"/>
	
	<!-- 5 : Should be valid : the key is associated with a
single value eg: 'foo' -->
	<mustHaveFooOrBar foo="foo"/>
	
	<!-- 6 : Should be valid : the key is associated with a
single value eg: 'bar' -->
	<mustHaveFooOrBar bar="bar"/>
	
	<!-- 7 : Should be invalid : the key is associated with 2
values!-->
	<mustHaveFooOrBar bar="bar" foo="foo"/>
	
	<!-- 8 : Should be invalid : the key is associated no value!-->
	<mustHaveFooOrBar>
		<elt>toto</elt>
	</mustHaveFooOrBar>
</test>
--------------------------------------------------------------------------------------

The comments reflects my understanding of the validation
process, as explained in the book I told about...My
interrogations come from the fact that a lot of parsers don't
give the same results for this exemple : 

- All the parsers don't validate the elements 4 and 8,
complaining that a key has no value, which is right,
- xerces (C or Java 2, last stable version available) does not
validate element 2, complaining that the key has no value,
whereas element 'elt' is defined. According to the book, it
should validate this content.
- xerces also validates elements 3 and 7, which should not be
valid, because there are two values for each key.

So, in fact, I really don't know what to think about all this,
but there are two possibilities : 

- Eric Van der Vlist is wrong, and Xerces is right...
- Xerces does not validate a valid XML content, and validates
some invalid content..

I'd really need to know who is right and who is wrong : I need
to write some schemas with such constraints, and I'd also like
to warn the one who's wrong!

Thanks for having read all this, and excuse my english mistakes!

Etienne Lacazedieu

Anyway, 


Accédez au courrier électronique de La Poste : www.laposte.net ; 
3615 LAPOSTENET (0,34/mn) ; tél : 08 92 68 13 50 (0,34/mn)

Received on Thursday, 18 March 2004 10:23:08 UTC