Groups | Blog | Home
all groups > dotnet xml > june 2004 >

dotnet xml : Problem with Schema Validations using XmlValidatingReade--HELP


Rajesh Jain
6/17/2004 9:04:01 PM
I Have 2 separate schemas.

--------------Schema 1 is defined as below-----------
<xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Loan">
<xs:complexType>
<xs:sequence>
<xs:element name="Borrower" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="BorrID" use="required">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/> <!--THIS IS WHAT CAUSES PROBLEMS -->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="BID">
<xs:selector xpath="Borrower"/>
<xs:field xpath="@BorrID"/>
</xs:unique>
</xs:element>
</xs:schema>

--------------END Schema 1-----------------------------

--------------Schema 2 is defined as below-----------
<xs:schema targetNamespace="http://Schemas/2" xmlns="http://Schemas/2" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="LEAD_INFO">
<xs:complexType>
<xs:sequence>
<xs:element name="Borrower" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="BorrID" use="required">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/> <!--THIS IS WHAT CAUSES PROBLEMS -->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

--------------END Schema 2-----------------------------

Notice the use of "minInclusive/maxInclusive" attribs on Borrower

For all intents and purposes, the two schemas were meant for different purposes. The third schema intends to tie the two schemas together as below

--------------Schema 3 is defined as below-----------
<xs:schema targetNamespace="http://Schemas/3" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:s3="http://Schemas/3" xmlns:s1="http://Schemas/1" xmlns:s2="http://Schemas/2" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://Schemas/2" schemaLocation="Schema2.xsd"/>
<xs:import namespace="http://Schemas/1" schemaLocation="Schema1.xsd"/>
<xs:element name="ROOT">
<xs:complexType>
<xs:sequence>
<xs:element ref="s1:Loan"/>
<xs:element ref="s2:LEAD_INFO"/>
</xs:sequence>
</xs:complexType>
<xs:keyref name="B2" refer="s3:B1">
<xs:selector xpath="s2:LEAD_INFO/s2:Borrower"/>
<xs:field xpath="@BorrID"/>
</xs:keyref>
<xs:key name="B1">
<xs:selector xpath="s1:Loan/s1:Borrower"/>
<xs:field xpath="@BorrID"/>
</xs:key>
</xs:element>
</xs:schema>

--------------END Schema 3-----------------------------

Note that I am trying to enforce Ref integrity using key/keyref


--------------The Sample XML is as below----------------
<ROOT xmlns="http://Schemas/3" xmlns:s1="http://Schemas/1" xmlns:s2="http://Schemas/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<Loan xmlns="http://Schemas/1">
<Borrower BorrID="1"/>
</Loan>
<LEAD_INFO xmlns="http://Schemas/2">
<Borrower BorrID="1"/>
</LEAD_INFO>
</ROOT>

--------------END SAMPLE-----------------------------------

The Code Snippet in C# is as below (I know the code is not perfect but please bear with me.)
-----------------------------------------------------------------
StreamReader s= System.IO.File.OpenText(@"C:\Work\ELIS\Sample.xml");
string xmlFrag =s.ReadToEnd();
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
reader.Schemas.Add("http://Schemas/3", @"file:///Schema3.xsd");
reader.ValidationType=ValidationType.Schema;
reader.ValidationEventHandler+=new ValidationEventHandler(ValidationEventHandle); //PLEASE ADD A VALIDATION HANDLER
while (reader.Read()){} //Loop through elements
//f you reach here without hitting validations, you are OK
MessageBox.Show("DONE");
----------------End Code Snippet------------------------------

The Problem is that when I run the code snippet, I get the error
"The key sequence '1' in Keyref fails to refer to some key. An error occurred at , (10, 4)." I checked the XML with XMLSPY and it does not report any validation problems.

Anyway, the moment I took the minInclusive/maxInclusive restrictions off, the error disappears.

The Schemas that work with the same Sample.Xml (and the same tieing schema (Schema3) are as below

--------------Schema 1 is defined as below-----------
<xs:schema targetNamespace="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://Schemas/1" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Loan">
<xs:complexType>
<xs:sequence>
<xs:element name="Borrower" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="BorrID" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="BID">
<xs:selector xpath="Borrower"/>
<xs:field xpath="@BorrID"/>
</xs:unique>
</xs:element>
</xs:schema>


--------------END Schema -----------


--------------Schema 2 is defined as below-----------
<xs:schema targetNamespace="http://Schemas/2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://Schemas/2" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="LEAD_INFO">
<xs:complexType>
<xs:sequence>
<xs:element name="Borrower" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="BorrID" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
--------------END Schema -----------

Notice that the simpletype under borrower are missing (For minInclusive/maxInclusive restriction)

Can you guys please help me.

Thanks in advance for all your help. To me it seems like a bug in .NET.
Rajesh
Rajesh Jain
6/23/2004 12:18:01 PM
Never mind I found the resolution. .NET1.1 seems to be (infact) correct in handling this situation (YET AGAIN!). XMLSPY handles this incorrectly. Basically, .NET thinks that the two simpletypes (introduced because of the minInclusive restriction) are different because a) They are in different namespaces and b) they have no relationship (See section 3.11.1 in XML Schema Part 1: Structures @ http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/).

The resolution is to make a new type (maybe in a different "Types" namespace) and then make the two borrowerid fields of this new type (by importing the "Types" namespace in the two schemas).

Thanks for all who were on this issue!
Rajesh

[quoted text, click to view]
AddThis Social Bookmark Button