Schema constraint validation
Posted: Thu Jul 15, 2010 12:10 am
Hi all,
... currently learning about schema constraints (Keys, KeyRefs). It looks strange to me that these constraints are not validated when checking the schema. Consider the following:
If I now generate a sample XML document such as
I get a validation error Identity Constraint error: identity constraint "KeyRef@1cd35c5" has a keyref which refers to a key or unique that is out of scope.
Start location: 11:14
This raises two questions for me:
with the folling sample document
I get cvc-identity-constraint.4.2.1.a: Element "Sub_1" has no value for the key "newKey".
Start location: 6:10
This is not very surprising, since the attribute was not declared 'required' in the schema. But again - why isn't this discovered at the schema validation level?
Next, I make this a required attribute and change Element Form Default to 'qualified':
The sample document
now validates despite the identical attributes, since the key is not name-spaced. I'm surprised again: the key xpath="Sub" has evidently nothing to chew on - still at schema validation level, nothing is reported ...
I (believe) I understand all the individual problems at schema level, but what I really wonder about: If I introduce some constraints and verify them, but later modify the schema: How do I consistently guarantee that the constraints are still valid?
The other day, I used a schema 'out of my daily live' as a training vehicle. The E142 schema referenced has made it to an IEEE standard. Still, I find that the constraints are not name-spaced and hence ineffective. So it appears I'm talking a 'real live problem' here?
Am I missing something? Is my request impossible to do or are there tricks to get it done?
Thanks for your help in understanding this!
Stefan
(George, the links to Costello really help! )
... currently learning about schema constraints (Keys, KeyRefs). It looks strange to me that these constraints are not validated when checking the schema. Consider the following:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="www.bla.com" xmlns:ns="www.bla.com">
<xs:element name="Top">
<xs:complexType>
<xs:sequence>
<xs:element name="Sub_1">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Sub_2">
<xs:complexType>
<xs:attribute name="SubID" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="newKey">
<xs:selector xpath="Sub_2a"/>
<xs:field xpath="@SubID"/>
</xs:key>
</xs:element>
<xs:element name="Sub_1a">
<xs:complexType>
<xs:sequence>
<xs:element name="Sub_2a">
<xs:complexType>
<xs:attribute name="SubIDPtr"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:keyref name="newKeyref" refer="ns:newKey">
<xs:selector xpath="Sub_2a"/>
<xs:field xpath="SubIDPtr"/>
</xs:keyref>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Top xmlns="www.bla.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.bla.com file:./TestConstraint_1.xsd">
<Sub_1>
<Sub_2/>
<Sub_2/>
</Sub_1>
<Sub_1a>
<Sub_2a/>
</Sub_1a>
</Top>
Start location: 11:14
This raises two questions for me:
- KeyRef@1cd35c5 has 'limited readability' to me as a human
- why can't this out-of-scope error not be detected at schema level?
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"
targetNamespace="www.bla.com" xmlns:ns="www.bla.com">
<xs:element name="Top">
<xs:complexType>
<xs:sequence>
<xs:element name="Sub_1">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Sub_2">
<xs:complexType>
<xs:attribute name="SubID" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="newKey">
<xs:selector xpath="Sub_2"/>
<xs:field xpath="@SubID"/>
</xs:key>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Top xmlns="www.bla.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.bla.com file:./TestConstraint_2.xsd">
<Sub_1 xmlns="">
<Sub_2/>
<Sub_2/>
</Sub_1>
</Top>
Start location: 6:10
This is not very surprising, since the attribute was not declared 'required' in the schema. But again - why isn't this discovered at the schema validation level?
Next, I make this a required attribute and change Element Form Default to 'qualified':
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="www.bla.com" xmlns:ns="www.bla.com">
<xs:element name="Top">
<xs:complexType>
<xs:sequence>
<xs:element name="Sub_1">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Sub_2">
<xs:complexType>
<xs:attribute name="SubID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="newKey">
<xs:selector xpath="Sub"/>
<xs:field xpath="@SubID"/>
</xs:key>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Top xmlns="www.bla.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.bla.com file:./TestConstraint_3.xsd">
<Sub_1>
<Sub_2 SubID="SubID1"/>
<Sub_2 SubID="SubID1"/>
</Sub_1>
</Top>
now validates despite the identical attributes, since the key is not name-spaced. I'm surprised again: the key xpath="Sub" has evidently nothing to chew on - still at schema validation level, nothing is reported ...
I (believe) I understand all the individual problems at schema level, but what I really wonder about: If I introduce some constraints and verify them, but later modify the schema: How do I consistently guarantee that the constraints are still valid?
The other day, I used a schema 'out of my daily live' as a training vehicle. The E142 schema referenced has made it to an IEEE standard. Still, I find that the constraints are not name-spaced and hence ineffective. So it appears I'm talking a 'real live problem' here?
Am I missing something? Is my request impossible to do or are there tricks to get it done?
Thanks for your help in understanding this!
Stefan
(George, the links to Costello really help! )