what is wrong with this?

S

stormogulen

Hi,

Can someone please help me explain, why this doesn't work? The problem is
the xs:key and xs:keyref. I can only reference the last of the
"afsnit"-nodes (number 302). Whenever I try to reference number 301, I get
the error: 'Undefined values for keyref identity constraint AfsnitFK' (using
xmlspy 2003).

Why, oh why?

----------------------------------------------------------------------------
--

<?xml version="1.0" encoding="UTF-8"?>
<kalkulation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="s.xsd">
<tilbudsliste>
<afsnit id="301">
<navn>a</navn>
</afsnit>
<afsnit id="302">
<navn>b</navn>
</afsnit>
</tilbudsliste>
<pris afsnit="302"/>
</kalkulation>

------------------------------
s.xsd --------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="afsnit">
<xs:complexType>
<xs:sequence>
<xs:element name="navn" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
<xs:key name="afsnitPK">
<xs:selector xpath="."/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
<xs:element name="tilbudsliste">
<xs:complexType>
<xs:sequence>
<xs:element ref="afsnit" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="pris">
<xs:complexType>
<xs:attribute name="afsnit" type="xs:string"/>
</xs:complexType>
<xs:keyref name="afsnitFK" refer="afsnitPK">
<xs:selector xpath="."/>
<xs:field xpath="@afsnit"/>
</xs:keyref>
</xs:element>
<xs:element name="kalkulation">
<xs:complexType>
<xs:sequence>
<xs:element ref="tilbudsliste"/>
<xs:element ref="pris" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
 
K

Klaus Johannes Rusch

stormogulen said:
Can someone please help me explain, why this doesn't work? The problem is
the xs:key and xs:keyref. I can only reference the last of the
"afsnit"-nodes (number 302). Whenever I try to reference number 301, I get
the error: 'Undefined values for keyref identity constraint AfsnitFK' (using
xmlspy 2003).

Why, oh why?

This location of the xs:key element in the schema gives the context node in
which the constraint holds, with the constraint as you have it defined, you can
even insert multiple afsnit elements with the same ID (since the key is only
defined for each element).

If you place the xs:key in the containing element with a selector selecting
afsnit, it will work just as you expect it:

<xs:element name="tilbudsliste">
<xs:complexType>
<xs:sequence>
<xs:element ref="afsnit" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:key name="afsnitPK">
<xs:selector xpath="afsnit" />
<xs:field xpath="@id" />
</xs:key>
</xs:element>

<xs:element name="afsnit">
<xs:complexType>
<xs:sequence>
<xs:element name="navn" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" />
</xs:complexType>
</xs:element>

(remainder of your XSD file unchanged)

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top