468,249 Members | 1,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

problems with loading different xsd's with same namespace to XmlSchemaCollection

Che
Greetings,

I am writing an application that uses an extendible XML file. in order
to validate that XML I use a main XSD and in additional - few more
extensions XSD's that uses the types in the main XSD as base types.

my clients can define their own XSD's extensions and use my generic
application to process their XML's.

The problem:
I use the same namespace in all of the XSD's ( both base and extensions
), the first time I load a Schema into the XmlSchemaCollection it works
fine, but on the next schemas , the XmlSchemaCollection just overwrite
the previous ( maybe because of the same namespace ) and I only remain
with the most previously schema loaded.

code:
string[] schemas = new string[2]{"SchemaA.xsd","SchemaB.xsd"};
for(int i=0 ;i< schemas.GetLength(0) ; i++)
{
XmlTextReader xsdReader =new XmlTextReader(schemas[i]);

XmlSchema schem = XmlSchema.Read(xsdReader ,new
ValidationEventHandler(SchemaHandler) );

collection.Add(schem);
}

SchemA.xsd :

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="MyOwnNS" elementFormDefault="qualified"
xmlns="MyOwnNS"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="BaseSchema.xsd"/>
<xs:complexType name="ExtendibleA_Type">
<xs:complexContent>
<xs:extension base="Base_Type">
<xs:sequence>
<xs:element name="blabla" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:element name="ExtendibleA" substitutionGroup="BaseElement"
type="ExtendibleA_Type" />
</xs:schema>

SchemaB.xsd :

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="MyOwnNS" elementFormDefault="qualified"
xmlns="MyOwnNS"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="BaseSchema.xsd"/>
<xs:complexType name="ExtendibleB_Type">
<xs:complexContent>
<xs:extension base="Base_Type">
<xs:sequence>
<xs:element name="whiwhi" type="xs:integer" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:element name="ExtendibleB" substitutionGroup="BaseElement"
type="ExtendibleB_Type" />
</xs:schema>

I didnt add the base schema but it has the same target namespace.
must I differ the schemas with different namespace's ?

Nov 12 '05 #1
2 2019


Che wrote:

I use the same namespace in all of the XSD's ( both base and extensions
), the first time I load a Schema into the XmlSchemaCollection it works
fine, but on the next schemas , the XmlSchemaCollection just overwrite
the previous ( maybe because of the same namespace ) and I only remain
with the most previously schema loaded.

code:
string[] schemas = new string[2]{"SchemaA.xsd","SchemaB.xsd"};
for(int i=0 ;i< schemas.GetLength(0) ; i++)
{
XmlTextReader xsdReader =new XmlTextReader(schemas[i]);

XmlSchema schem = XmlSchema.Read(xsdReader ,new
ValidationEventHandler(SchemaHandler) );

collection.Add(schem);
I didnt add the base schema but it has the same target namespace.
must I differ the schemas with different namespace's ?


It depends on what you want to achieve, if you want to have all elements
in the same namespace then of cause you need the same target namespace
but then you need to compose the schemas with xs:include as needed. As
for the schema collection, indeed you can only add one schema per target
namespace there but if that uses xs:include to include other schemas
then these are loaded (depending on the trust of your application) as
well as far as needed to validate an XML instance documents.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2


Thanks Martin for your reply,

What I am trying to achieve is being able to validate a XML document
with a few (more than one) XSD's.
The requested XML use data elements from types that are being define in
a certain XSD but consist on base types that define in another basic
XSD.

It is important for me to allow every application client to create its
own types and elements that extend base types from the Base XSD.
That helps my application to load in additional to the base XSD a group
of unknown XSD's for the extensions, the XmlValidationReader could
validate all types from the XML relying on all required XSD'd.
For example:
myCar.xml describe a certain car, the car has a child element: engine,
every different car has a different type of engine.
Each XML that describe a car type need a different XSD for validation
according to the car engine type. I am doing that by giving my clients
the ability to derive their own engine types from a base type define in
my generalCar.xsd . In C# code I am loading the base XSD and a bunch of
XSD's (extensions) from a general location (all clients know to put
their XSD's there). Now I am validating the given XML ( without knowing
the derived types in advance ) with the XmlValidationReader that include
the schema collection with all XSD's.
I expect to have full validation ( and already succeeded ..).
The problem is that I canít deal with a different target namespace for
each extension.
The XmlScemaCollection cant load more than one XSD with the same
namespace.

Can I do it in such way or must I use different target namespaces?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Bob Rosen | last post: by
1 post views Thread by Mark Olbert | last post: by
4 posts views Thread by Aayush Puri | last post: by
9 posts views Thread by Eskimo | last post: by
reply views Thread by Stefan Christmann | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.