By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Does XmlValidatingReader check key and keyref constrains?

P: n/a
Hi

I have a question
whether XmlValidatingReader doesn't check keyref constrain (the same with
key constraint) or I do smth wrong.

I have the following schema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="module" targetNamespace="http://tempuri.org/module.xsd"
xmlns="http://tempuri.org/module.xsd"
xmlns:mstns="http://tempuri.org/module.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="module">
<xs:complexType>
<xs:sequence>
<xs:sequence>
<xs:element ref="type" maxOccurs="unbounded" />
<xs:element ref="object" maxOccurs="unbounded" />
</xs:sequence>
</xs:sequence>
</xs:complexType>
<xs:key name="typeNameKey">
<xs:selector xpath=".//type" />
<xs:field xpath="@name" />
</xs:key>
<xs:key name="objectNameKey">
<xs:selector xpath=".//object" />
<xs:field xpath="@name" />
</xs:key>
<xs:keyref name="typeOfObject" refer="typeNameKey">
<xs:selector xpath=".//object" />
<xs:field xpath="@ofType" />
</xs:keyref>
</xs:element>
<xs:element name="type">
<xs:complexType>
<xs:sequence />
<xs:attribute name="name" type="xs:NMTOKEN" />
</xs:complexType>
</xs:element>
<xs:element name="object">
<xs:complexType>
<xs:sequence />
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:attribute name="ofType" type="xs:NMTOKEN" />
</xs:complexType>
</xs:element>
</xs:schema>

and read the file

<?xml version="1.0" encoding="utf-8" ?>
<module xmlns="http://tempuri.org/module.xsd">
<type name="A" />
<type name="B" />
<object name="o1" ofType="A" />
<object name="o2" ofType="A" />
<object name="o3" ofType="B" />
<object name="o4" ofType="B" />
<object name="o4" ofType="C" />
</module>

with the code

XmlTextReader reader = new XmlTextReader(@"../../module1.xml");
XmlValidatingReader vreader = new XmlValidatingReader(reader);
vreader.Schemas.Add(@"http://tempuri.org/module.xsd",
@"../../module.xsd");
vreader.ValidationType = ValidationType.Schema;
vreader.ValidationEventHandler += new
ValidationEventHandler(vreader_ValidationEventHand ler);

XmlDocument doc = new XmlDocument();
doc.Load(vreader);

But I don't get an error for <object name="o4" ofType="C" />.
So the question is whether XmlValidatingReader doesn't check keyref
constrain (the same with key constraint) or I do smth wrong.

Dima.
Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
The XPath expression in the selectors are not qualified with the
targetNamespace of the schema.
The XPath expression ".//object" selects object elements that not namespace
qualified, whereas you want the xpath to select all
http://tempuri.org/module.xsd:object elements

Changing the selectors to the following produces the following errors:
<xs:key name="typeNameKey">
<xs:selector xpath="mstns:type" />
<xs:field xpath="@name" />
</xs:key>
<xs:key name="objectNameKey">
<xs:selector xpath="mstns:object" />
<xs:field xpath="@name" />
</xs:key>
<xs:keyref name="typeOfObject" refer="typeNameKey">
<xs:selector xpath="mstns:object" />
<xs:field xpath="@ofType" />
</xs:keyref>

Exception Severity: Error
There is a duplicate key sequence 'o4' for
'http://tempuri.org/module.xsd:objectNameKey' key or unique identity
constraint.
An Error Occurred at: file:///D:/BugRepro/keyref.xml, (9,6)

Exception Severity: Error
The key sequence 'C' in Keyref fails to refer to some key.
An Error Occurred at: file:///D:/BugRepro/keyref.xml, (9,6)
5/18/2004 1:06:41 PM

Thanks,
Priya

"Dmitry Martynov" <do********@hotmail.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
Hi

I have a question
whether XmlValidatingReader doesn't check keyref constrain (the same with
key constraint) or I do smth wrong.

I have the following schema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="module" targetNamespace="http://tempuri.org/module.xsd"
xmlns="http://tempuri.org/module.xsd"
xmlns:mstns="http://tempuri.org/module.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="module">
<xs:complexType>
<xs:sequence>
<xs:sequence>
<xs:element ref="type" maxOccurs="unbounded" />
<xs:element ref="object" maxOccurs="unbounded" />
</xs:sequence>
</xs:sequence>
</xs:complexType>
<xs:key name="typeNameKey">
<xs:selector xpath=".//type" />
<xs:field xpath="@name" />
</xs:key>
<xs:key name="objectNameKey">
<xs:selector xpath=".//object" />
<xs:field xpath="@name" />
</xs:key>
<xs:keyref name="typeOfObject" refer="typeNameKey">
<xs:selector xpath=".//object" />
<xs:field xpath="@ofType" />
</xs:keyref>
</xs:element>
<xs:element name="type">
<xs:complexType>
<xs:sequence />
<xs:attribute name="name" type="xs:NMTOKEN" />
</xs:complexType>
</xs:element>
<xs:element name="object">
<xs:complexType>
<xs:sequence />
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:attribute name="ofType" type="xs:NMTOKEN" />
</xs:complexType>
</xs:element>
</xs:schema>

and read the file

<?xml version="1.0" encoding="utf-8" ?>
<module xmlns="http://tempuri.org/module.xsd">
<type name="A" />
<type name="B" />
<object name="o1" ofType="A" />
<object name="o2" ofType="A" />
<object name="o3" ofType="B" />
<object name="o4" ofType="B" />
<object name="o4" ofType="C" />
</module>

with the code

XmlTextReader reader = new XmlTextReader(@"../../module1.xml");
XmlValidatingReader vreader = new XmlValidatingReader(reader);
vreader.Schemas.Add(@"http://tempuri.org/module.xsd",
@"../../module.xsd");
vreader.ValidationType = ValidationType.Schema;
vreader.ValidationEventHandler += new
ValidationEventHandler(vreader_ValidationEventHand ler);

XmlDocument doc = new XmlDocument();
doc.Load(vreader);

But I don't get an error for <object name="o4" ofType="C" />.
So the question is whether XmlValidatingReader doesn't check keyref
constrain (the same with key constraint) or I do smth wrong.

Dima.

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.