Page 1 of 1

Trouble validating a combined document

Posted: Fri Sep 23, 2005 3:08 am
by SSteve
Here's another validation problem I'm having. I combined two XML documents into one contained in a root element. Here's the outline:

Code: Select all


<?xml version="1.0" ?>

<Stock>

<gr:Grills xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gr="http://grillrite.com/grills"
xsi:schemaLocation="http://grillrite.com/grills GRSchema.xsd">
[valid contents here]
</gr:Grills>

<pt:Parts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:pt="http://grillrite.com/parts"
xsi:schemaLocation="http://grillrite.com/parts PTSchema.xsd">
[valid contents here]
</pt:Parts>

</Stock>
Oxygen gives this error message:

[red]http://www.w3.org/TR/xmlschema-1/#cvc-elt E cvc-elt.1: Cannot find the declaration of element 'Stock'. [/red]

Since the Stock tag is outside of any schema, shouldn't it be valid? XMLSpy doesn't complain about this file. I imagine there's something I'm doing wrong or a setting I'm not familiar with.

Posted: Fri Sep 23, 2005 9:01 am
by Radu
Hi Steve,

The XML file you quoted above is not a valid XML document because the root element "Stock" is declared neither in a DTD nor in a schema.

According to the Xerces J parser used to validate Oxygen files:
The schema is specified by the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute on the root element of the document. The xsi prefix must be bound to the Schema document instance namespace, as specified by the Recommendation.
So declaring schema locations for elements other than the root is an implementation limitation of the Xerces parser, see http://xml.apache.org/xerces-j/schema.html.

The same error Oxygen reports is reported in XML Spy when clicking the "Validate" button located on the toolbar.


Regards, Radu.

Posted: Fri Sep 23, 2005 3:46 pm
by Radu
Hi Steve,

Ignore my previous remark about the Xerces J limitation regarding the attribute xsi:schemaLocation appearing only in the root element.
That link was to an older Xerces implementation. With the Xerces available in the latest version of Oxygen, declaring different schema locations for different elements in the xml file is possible.
So basically all you have to do is declare a schema location for the root element of the xml file.

Regards, Radu.

Posted: Fri Sep 23, 2005 4:15 pm
by george
Hi,

Here it is a set of files that shows a working sample.

test.xml

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<t:test xmlns:t="test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="test test.xsd">
<a:a xmlns:a="a" xsi:schemaLocation="a a.xsd" a="1"/>
<a:b xmlns:a="b" b="2" xsi:schemaLocation="b b.xsd"/>
</t:test>
test.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test">
<xs:element name="test">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:any processContents="strict"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
a.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="a">
<xs:element name="a">
<xs:complexType>
<xs:attribute name="a" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
b.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="b">
<xs:element name="b">
<xs:complexType>
<xs:attribute name="b" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Best Regards,
George

Posted: Fri Sep 23, 2005 8:51 pm
by SSteve
But is it possible to have no schema or namespace for the root element while having different schemas and namespaces for the two child elements? I'm taking an XML class and the whole point of this particular exercise is that it's easy to combine two XML documents with separate schemas/namespaces into one document by using the form in my original post. According to the book (and the older version of XMLSpy that comes with the book), the code I posted is valid. But it wouldn't be the first time the book was wrong. :-)

Posted: Fri Sep 23, 2005 10:23 pm
by george
Hi Steve,

When you talk about validation that makes sense only if you have a schema or a DTD to validate against, there is no point to talk about validation otherwise.

Best Regards,
George

Posted: Fri Sep 23, 2005 10:28 pm
by SSteve
Oh, of course. All the concepts are still settling into my tiny brain. :-)

Thanks so much for your help.