Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : problems with loading different xsd's with same namespace to XmlSchemaCollection


Che
2/20/2005 7:28:38 AM
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 ?
Martin Honnen
2/20/2005 4:41:15 PM


[quoted text, click to view]


[quoted text, click to view]


[quoted text, click to view]

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
che G
2/20/2005 10:31:52 PM


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 ***
AddThis Social Bookmark Button