all groups > dotnet xml > august 2005 >
You're in the

dotnet xml

group:

namespace problem during validation


namespace problem during validation tomek.romanowski NO[at]SPAM gmail.com
8/13/2005 6:13:23 AM
dotnet xml: 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 :-(
Re: namespace problem during validation Priscilla Walmsley
8/13/2005 3:23:24 PM
Hi,

You need to put elementFormDefault="qualified" on your incr11.xsd schema.
Otherwise, all locally declared elements (like firstname, lastname, etc.)
are in NO namespace. In your sample instance document, they are in the
tnsperson namespace, because the default namespace declaration on the
"person" element applies to them.

So the error message makes sense - it is finding firstname in the tnsperson
namespace, when it is expecting to find firstname in no namespace.

Hope that helps,
Priscilla

------------------------------------------------------------------
Priscilla Walmsley
Author, Definitive XML Schema / XML in Office 2003
http://www.datypic.com
------------------------------------------------------------------

[quoted text, click to view]

Re: namespace problem during validation tomek.romanowski NO[at]SPAM gmail.com
8/15/2005 12:29:11 AM

Priscilla !!!

Thanks for your help !!!
AddThis Social Bookmark Button