473,388 Members | 1,340 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,388 software developers and data experts.

Validating XML against XSD where XSD has includes

I have 2 Xsd's

The one contains basic type definitions

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="TrueFalse">
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

The other includes the previous XSD so that the types declared can be used

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Elements.xsd"/>
<xs:element name="Events">
<xs:complexType>
<xs:sequence>
<xs:element name="Source">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="IsEstimatedDate" type="TrueFalse" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If I do this I can validate XML against the schema as below:
Validator.Schema.Add("", @"C:\XSD\Event.xsd");

Validation Code:
XmlParserContext Context = new XmlParserContext(null, null, "",
XmlSpace.None);
XmlValidatingReader Reader = new XmlValidatingReader(XMLStream,
XmlNodeType.Element, Context);
Reader.ValidationType = ValidationType.Schema;
Reader.ValidationEventHandler += new
ValidationEventHandler(ShowCompileErrors);
Reader.Schemas.Add(Schema);
etc

Validation works - no problem.
I assume that .Net knows how to find the Elements.xsd in the above case -
becuase the Elements xsd sits in the same dir at the events xsd

Then:
I want to include the XSD's as an embedded resource in my .dll I have done
this, and I have streamed the XSD

Running
XmlTextReader Xsdreader = new XmlTextReader(EventsXsdStream);
Validator.Schema.Add("", Xsdreader);

Result
Type 'TrueFalse' is not declared.

I then try to add the Elements.Xsd to the Schema collection before I add the
XmlTextReader Xsdreader = new XmlTextReader(ElementsXsdStream);
Validator.Schema.Add("", Xsdreader);

Result
Type 'TrueFalse' is not declared.

How should I load both XSD's so that the Events.xsd knows about the
Elements.xsd and so that my validation will work correctly?

Thank you

--------------------------------
From: Brendon
Nov 12 '05 #1
1 5967
When you added Events.xsd from a file, the xs:include directive allowed it
to access components from Elements.xsd which was resolved using the baseURI
of Events.xsd, it worked since both schemas were in the same directory.

But now since you are loading them through a Stream, the basURI of
Events.xsd is not set, and it does not know where to resolve Elements.xsd
reference from. Notice that when you Add Events.xsd from a dll source as:
XmlTextReader Xsdreader = new XmlTextReader(EventsXsdStream);
Validator.Schema.Add("", Xsdreader);
it must through a validation warning through the event handler with the
error that schemaLocation could not be resolved, since it could not access
the Elements.xsd schema. In XmlSchemaCollection, you can add only one schema
from a namespace to the collection using Add ( ), so you can not Add both
schemas one by one to the collection and have it all work for you.

If you have both XSD sources in your DLL, you can load up both schemas in
XmlSchema objects using:

XmlSchema schema = XmlSchema.Read(new StreamReader(),
validationEventHandler);

and then set the schema.Includes property of the Events schema to point to
the Elements schema and then call schema.compile() for the Events schema.

In the .net framwork v2.0, a new schema store namely XmlSchemaSet is
present, which allows multiple adds of the schemas of the same namespace,
and this problem would not be present there.
Thanks.


"Brendon" <br*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... I have 2 Xsd's

The one contains basic type definitions

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="TrueFalse">
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

The other includes the previous XSD so that the types declared can be used

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Elements.xsd"/>
<xs:element name="Events">
<xs:complexType>
<xs:sequence>
<xs:element name="Source">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="IsEstimatedDate" type="TrueFalse" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If I do this I can validate XML against the schema as below:
Validator.Schema.Add("", @"C:\XSD\Event.xsd");

Validation Code:
XmlParserContext Context = new XmlParserContext(null, null, "",
XmlSpace.None);
XmlValidatingReader Reader = new XmlValidatingReader(XMLStream,
XmlNodeType.Element, Context);
Reader.ValidationType = ValidationType.Schema;
Reader.ValidationEventHandler += new
ValidationEventHandler(ShowCompileErrors);
Reader.Schemas.Add(Schema);
etc

Validation works - no problem.
I assume that .Net knows how to find the Elements.xsd in the above case -
becuase the Elements xsd sits in the same dir at the events xsd

Then:
I want to include the XSD's as an embedded resource in my .dll I have done
this, and I have streamed the XSD

Running
XmlTextReader Xsdreader = new XmlTextReader(EventsXsdStream);
Validator.Schema.Add("", Xsdreader);

Result
Type 'TrueFalse' is not declared.

I then try to add the Elements.Xsd to the Schema collection before I add the XmlTextReader Xsdreader = new XmlTextReader(ElementsXsdStream);
Validator.Schema.Add("", Xsdreader);

Result
Type 'TrueFalse' is not declared.

How should I load both XSD's so that the Events.xsd knows about the
Elements.xsd and so that my validation will work correctly?

Thank you

--------------------------------
From: Brendon

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Will | last post by:
I have been having problems validating an XForms document against the XForms schema located at http://www.w3.org/MarkUp/Forms/2002/XForms-Schema.xsd. I have reduced the XForm to its bare bones...
6
by: COHENMARVIN | last post by:
I'm just starting out in XML for my business, and already I'm given a large and complicated XML schema. If I write XML files based on the schema, is there any tool that 1. Tells me if I'm valid...
1
by: davisjoseph | last post by:
Hi All, I'm using Xerces C++ 2.50 DOM version for Validating a valid XML againt the Schema(XSD) available; But it always shows an error in XSD I suppose; This is the error I'm getting using...
30
by: Toni Mcintyre | last post by:
i'm having 2 problems with the http://validator.w3.org 1. if i have: <meta http-equiv="Content-Script-Type" content="text/javascript"> then why do i need <script type=text/javascript>...
1
by: Christian | last post by:
Hi, I load an Xml-file "customers.xml" into a DataSet (works fine) but then how do I validate it against a schema (e.g. customers.xsd) ? my customers.xml: <?xml version="1.0"...
0
by: RJN | last post by:
Hi My web service receives an object of type say MyObject. I want to serialize this object,and then validate the xml against the main xsd. When validation happens, it should also validate...
0
by: RJN | last post by:
Hi The main schema includes one more schema and the actual types are described in the included schema. eg., <xs:schema xmlns:t="http://myschemas/base" xmlns=""> <xs:import...
0
by: RJN | last post by:
hi My web service receives an object of type say MyObject. I want to serialize this object,and then validate the xml against the main xsd. When validation happens, it should also validate...
7
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
I wrote a method to validate and xml file against a schema. If the file does not conform to the schema, it throws an error. It works fine except for one curious thing. If I try to validate an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.