xsd:attribute use="required" validation question

Having trouble installing Oxygen? Got a bug to report? Post it all here.
thatch
Posts: 4
Joined: Fri Oct 31, 2003 12:48 am

xsd:attribute use="required" validation question

Post by thatch »

Hi,
I am a new trial user with the latest eclipse 3.0.0 and oxygenxml plugin for eclipse. I have written several schemas, lets call the first components.xsd and the later structure.xsd. The first looks something like:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="myNamespace"
xmlns:"myNamespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="qualified">

<xsd:attribute name="guid" type="xsd:int"/>

</xsd:schema>

The second looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="myNamespace"
xmlns:"myNamespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="qualified">

<xsd:include schemaLocation="component.xsd"/>

<xsd:element name="object">
<xsd:complexType>
<xsd:attribute ref="guid" use="required"/>
</xsd:complexType>
</xsd:element>

</xsd:schema>

Both schemas are well-formed and validate as proper xsd. When I create a new xml file which points to that schema for validation, in this case object must be the root and my xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<object xmlns:"myNamespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="myNamespace file:/home/me/xml/schema/structure.xml"
guid="1234">
</object>

When I validate I get two error messages, one says:
cvc-complex-type.3.2.2: Attribute 'guid' is not allowed to appear in element 'object'.

While the other says:
cvc-complex-type.4: Attribute 'guid' must appear in element 'object'.

This behavior happens at all levels of my xml for more complex schema, ie not just for attributes in the root node, and it doesn't happen if the attribue is optional; however, I tried to simplify it to the most basic case. The interesting thing is that the autocompletion works perfectly less the attribute for the root node. I may be doing something stupid though, as it woluldn't be the first time

Thanks for any insight.

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

Post by george »

Dear Thatcher,

When you define a top level attribute it belongs to the schema target namespace, in your case the target namespace is "myNamespace". In order to enter an attribute from a specified namespace you should qualify it with a prefix pointing to that namespace, if no prefix is specified the attribute belongs to no namespace. Thus if you change your sample to look like:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?> 
<object xmlns="myNamespace" xmlns:x="myNamespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="myNamespace structure.xsd"
x:guid="1234"></object>
You will not get the attribute related error anymore.

I admit the error message is not very helpful in this case, the first error should say that the guid attribute from no namespace is not allowed and the second error should say that attribute guid from the "myNamespace" namespace must appear.

Best Regards,
George
thatch
Posts: 4
Joined: Fri Oct 31, 2003 12:48 am

Post by thatch »

George,
Thanks for your reply, however, I'm thoroughly confused. I have been searching the web for a definative answer but I can't find one. So I'll ask here even though I'm sure you are tired of correcting xml mistakes instead of supporting real oxygen questions. Here it is:


I thought that by specifying xmlns="myNamespace" that all elements without a namespace prefix use this namespace, and that all attributes use the elements namespace unless excplicitly prefixed by another. Am I mistaken?

Thanks again, your team has created a very useful tool, and unfortunatly is helping me discover just how little xml I really know.

Thatcher
thatch
Posts: 4
Joined: Fri Oct 31, 2003 12:48 am

Post by thatch »

Sorry George,

I finally found it, you are correct. Attributes have no namespace, so all attributes from a given namespace must be prefixed. Hope this helps someone else also.

Thatcher
Post Reply