namespace problem during validation
- From: tomek.romanowski@xxxxxxxxx
- Date: 13 Aug 2005 06:13:23 -0700
Hi !
I have problem with validating of the document with multiple
namespaces. The odd thing is, that my data work O'K when I test it
under XMLSpy but it doesn't work with my C# code.
My first 'main' schema is as follows:
incr2.xsd
----------
<xs:schema targetNamespace="http://vit.volvo.com/dept9272/2004/ESB"
xmlns:p="tnsperson" xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="tnsperson" schemaLocation="incr11.xsd"/>
<xs:element name="record">
<xs:complexType >
<xs:sequence>
<xs:element ref="p:person"/>
<xs:element name="info" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
As you can see, it imports another schema,
incr11.xsd
----------
<xs:schema targetNamespace="tnsperson" xmlns:t = "tnsperson"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="persontype">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="person" type="t:persontype">
</xs:element>
</xs:schema>
The sample document is here:
sample.xml
-----------------
<?xml version="1.0" encoding="UTF-8"?>
<record xmlns="http://vit.volvo.com/dept9272/2004/ESB"
xmlns:p="tnsperson" >
<person xmlns="tnsperson">
<firstname>Tom</firstname>
<lastname>Rom</lastname>
<age>22</age>
</person>
<info>x_info</info>
</record>
The error message is as follows:
*****************
The element 'tnsperson:person' has invalid child element
'tnsperson:firstname'. Expected 'firstname'. An error occurred at file
sample.xml, (4, 4).
*****************
I make the validation using the code presented below. The 'filename' is
XML doc filename, 'SchemaURL' xsd filename and TargetNamespace has
http://vit.volvo.com/dept9272/2004/ESB as a value.
reader = new XmlTextReader(filename);
reader.WhitespaceHandling=WhitespaceHandling.None;
// ^^-- to avoid problems when validating indented docs
XmlSchemaCollection xsc = null;
xsc = new XmlSchemaCollection();
xsc.Add(TargetNamespace, SchemaURL);
vreader = new XmlValidatingReader(reader);
vreader.Schemas.Add(xsc);
vreader.ValidationEventHandler += new
ValidationEventHandler(this.ValidationCallback);
while (vreader.Read())
{}
Why the parser expects child element from default namespace, when its
parent is declared as a reference from other namespace ?
Thanks in advance for any help. I'e spent two days browsing and
testing. No luck yet :-(
.
- Follow-Ups:
- Re: namespace problem during validation
- From: Priscilla Walmsley
- Re: namespace problem during validation
- Prev by Date: Re: The root element is missing
- Next by Date: Re: The root element is missing
- Previous by thread: The root element is missing
- Next by thread: Re: namespace problem during validation
- Index(es):
Relevant Pages
|