What is wrong with my schema?

This should cover W3C XML Schema, Relax NG and DTD related problems.
tom
Posts: 3
Joined: Mon Feb 07, 2005 11:37 pm

What is wrong with my schema?

Post by tom » Mon Feb 07, 2005 11:44 pm

xml:

Code: Select all


<?xml version="1.0" encoding="ISO-8859-1"?>
<data xmlns="http://www.example.net/test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.net/test test.xsd">
<foo>dfgsdgf</foo>
<bar>blabla</bar>
</data>
and the schema (automaticaly generated, I only added targetNamespace="...")

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.example.net/test">
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element ref="foo"/>
<xs:element ref="bar"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="foo" type="xs:NCName"/>
<xs:element name="bar" type="xs:NCName"/>
</xs:schema>
If I try to validate the schema, I get this:
src-resolve.4.1: Error resolving component 'foo'. It was detected that 'foo' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/D:/eclipse/workspace/test/test.xsd'. If 'foo' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'foo' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:/D:/eclipse/workspace/test/test.xsd'.
@see: http://www.w3.org/TR/xmlschema-1/#src-resolve
and if I try to validate the xml, I get this:
cvc-complex-type.2.4.d: Invalid content was found starting with element 'foo'. No child element is expected at this point.
@see: http://www.w3.org/TR/xmlschema-1/#cvc-complex-type
What am I doing wrong?
I don't understand what the validator wants from me :?

thanks
tom

tom
Posts: 3
Joined: Mon Feb 07, 2005 11:37 pm

Post by tom » Mon Feb 07, 2005 11:52 pm

I think I've found the problem, I haveto add xmlns="http://www.example.net/test" to the schema. But why?

I don't understand it :?:
Is there a situation when targetNamespace and xmlns are different (in a xml schema)?

thanks
tom

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Feb 08, 2005 11:28 am

Yes, target namespace and default namespace can be different in a schema, your schema for instance.

Target namespace appers when you define an element for instance. The element name specifies the local name of the declared element, the namespace is determined from the target namespace.

When you refer an element declaration on the other hand you are not using target namespace, you must specify a QName (qualified name) as the value of the element ref attribute and that depends on the namespace context and here the default namespace may be important. I said may because you can have for instance your schema written like

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.example.net/test"
xmlns:t="http://www.example.net/test">
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element ref="t:foo"/>
<xs:element ref="t:bar"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="foo" type="xs:NCName"/>
<xs:element name="bar" type="xs:NCName"/>
</xs:schema>
and you will also have a valid schema.

Best Regards,
George

tom
Posts: 3
Joined: Mon Feb 07, 2005 11:37 pm

Post by tom » Tue Feb 08, 2005 5:08 pm

Thanks..
Now I think I got it.. how schema works

tom

Post Reply