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

Problem with Schema Validations using XmlValidatingReade--HELP

P: n/a
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

Nov 12 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.