473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

Does XmlValidatingReader check key and keyref constrains?

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
1 2821
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: peterpeter | last post by:
Hi. There is a XML schema problem that I have with key/keyref: I have two complex (A and B) types which both inherit from a common base typ (Base). A refers B using a xs:IDREF element named...
0
by: Markus Seibold | last post by:
Hello NG, sorry for pasting that much code but I just can't figure out how get the <key><keyref> to work in XML Schema. I am using XMLSpy 4.3 for editing my XML. I want to represent a...
5
by: Russell O'Connor | last post by:
The following XML and Schema doesn't validate in Visual Studio .NET 2003. Is there some mistake I'm making, or is VS.NET in error. The error is r:\test2.xml(3): The key sequence 'foo' in Keyref...
0
by: mark.van.der.voort | last post by:
Hi, I have defined a simple key/keyref validation for describing a tree. <xs:key name="key_member"> <xs:selector xpath=".//member"/> <xs:field xpath="@id"/> </xs:key> <xs:keyref...
18
by: Vlad | last post by:
I have the following code: I have a local copy of the DTD that I need to validate incoming XML documents against. The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define. When the...
2
by: svestin | last post by:
Hi All! I run into a problem defining a XSD schema with KEYREF references. Is it possible to use KEYREF with nillable fields? Just like a database where a FK could be null. In the example...
1
by: Wolfgang Uhr | last post by:
Hello I have the following problem. I want to use the validating parser XmlValidatingReader and so I write ... FileStream stream = new FileStream(ofd.FileName, FileMode.Open);...
0
by: mavis | last post by:
Does IntellJ support the checking for "keyref" definition? Why it seems does not work at all? <xs:key name="supported-platforms-name-unique"> <xs:selector...
0
by: peterpeter | last post by:
Hi. There is a XML schema problem that I have with key/keyref: I have a complex type "AExtended" which inherits from a base type "ABase". Both are allowed to be instantiated under the "Root"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.