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

Validating XML against XSD where XSD has includes

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.