Click to See Complete Forum and Search --> : XSD+key works but XSD+namespace+key not works


marcintom
11-30-2008, 10:38 AM
I have :
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > <!-- xmlns="http://xx.pl/marcin" targetNamespace="http://xx.pl/marcin"> -->
<xs:annotation>
<xs:documentation xml:lang="pl">
Przykład schematu z rozszerzaniem typów i referencjami.
</xs:documentation>
</xs:annotation>
<xs:element name="studenci">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="student" type="StudentTyp"/>
<xs:element name="osoba" type="OsobaTyp"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="OsobaTyp">
<xs:sequence>
<xs:element name="imie" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="nazwisko" type="xs:string"/>
</xs:sequence>
<xs:attribute name="plec" type="PlecTyp" use="required"/>
<xs:attribute name="email" type="xs:string" use="optional"/>
</xs:complexType>
<xs:complexType name="StudentTyp">
<xs:complexContent>
<xs:extension base="OsobaTyp">
<xs:sequence>
<xs:element name="nr-indeksu" type="xs:unsignedLong"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:simpleType name="PlecTyp">
<xs:restriction base="xs:string">
<xs:enumeration value="k"/>
<xs:enumeration value="m"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="grupy">
<xs:complexType>
<xs:sequence>
<xs:element name="grupa" type="GrupaTyp" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="GrupaTyp">
<xs:sequence>
<xs:element name="numer-grupy" type="xs:positiveInteger"/>
<xs:element name="student" type="StudentWGrupieTyp" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="StudentWGrupieTyp">
<xs:attribute name="ref" type="xs:string"/>
</xs:complexType>



<xs:element name="uczelnia">
<xs:complexType>
<xs:sequence>
<xs:element ref="studenci"/>
<xs:element ref="grupy"/>
</xs:sequence>
</xs:complexType>
<xs:key name="studenci_key">
<xs:selector xpath="studenci/student"/>
<xs:field xpath="nr-indeksu"/>
</xs:key><!--
<xs:keyref name="grupy_studenci_ref" refer="studenci_key">
<xs:selector xpath="grupy/grupa/student"/>
<xs:field xpath="@ref"/>
</xs:keyref>-->
</xs:element>


</xs:schema>

and this work fine if the "nr-indeksu" is unique but if nr-indeksu is not unique there is an error in xml document
So this is good.

But when I tried to add xmlns="http://xx.pl/marcin" targetNamespace="http://xx.pl/marcin" to schema
The example xml documents with the same nr-indeksu is valid.
This is bad.
What can be wrong in my document.

rpgfan3233
11-30-2008, 12:40 PM
From W3Schools' XML Schema key Element (http://www.w3schools.com/schema/el_key.asp) page:
The key element MUST contain the following (in order):

* one and only one selector element (contains an XPath expression that specifies the set of elements across which the values specified by field must be unique)
* one or more field elements (contains an XPath expression that specifies the values that must be unique for the set of elements specified by the selector element)

Notice the second point. The field element contains an XPath expression specifying values that must be unique for the elements selected. In other words, if you use the same value twice in a nr-indeksu element, your XML document is invalid. The same is true if you used xml:id more than once, though that's just a rule of XML, not a rule of XML Schema. :p

I created an XML document based on the XML schema you posted above and received an error similar to that. If you can explain what you're trying to do, I might be able to help you figure out something that works.

Also, you may want to change your <xs:schema> tag:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" > <!-- xmlns="http://xx.pl/marcin" targetNamespace="http://xx.pl/marcin"> -->

Without the elementFormDefault="qualified" attribute-value pair, your XML schema won't work properly because your element names aren't bound to a namespace. For example, I use the lxml.etree module for Python (a programming language) to validate everything. Without elementFormDefault being "qualified", here is the error:
lxml.etree.XMLSyntaxError: Element '{http://xx.pl/marcin}student': This element is not expected. Expected is one of ( student, osoba ).

In my XML document, the elements are qualified with the 'http://xx.pl/marcin' namespace thanks to xmlns attribute on the uczelnia element. If I remove the xmlns attribute from the XML document (not the XML schema), then I get a new error:
lxml.etree.XMLSyntaxError: Element 'uczelnia': No matching global declaration available for the validation root.

So that elementFormDefault attribute is VERY important! ^_^

marcintom
11-30-2008, 03:16 PM
Thanx for your interesting

I solve this problem

XMLSpy editor not validate the key/keyref

In other validate tool everything is ok.