469,361 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,361 developers. It's quick & easy.

XSD Schema - nillable fields in keyref

Hi All!

I run into a problem defining a XSD schema with KEYREF references.

Is it possible to use KEYREF with nillable fields?
Just like a database where a FK could be null.

In the example below the tag <Owner> is nillable
and also used as FIELD in the KEYREF.

Run this in Visual Studio 2005 XML Editor and there will be NO error.
Run this in XMLSpy and there WILL BE an error.

Anyone out there with any suggestions or workarounds?

Regards
/Stefan Vestin

<== FILENAME=KeyRef.xml ==>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="testNS KeyRef.xsd" xmlns="testNS">

<Persons>
<Person ID="123">
<Name>John</Name>
</Person>
<Person ID="456">
<Name>Frank</Name>
</Person>
</Persons>

<Vehicles>
<Vehicle ID="ABC">
<Owner xsi:nil="true"></Owner> <=== Reference that is NULL
</Vehicle>
</Vehicles>

</root>
<== FILENAME=KeyRef.xsd ==>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="testNS"
targetNamespace="testNS" elementFormDefault="qualified"

attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Persons">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Vehicles">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Vehicle">
<xs:complexType>
<xs:sequence>
<xs:element name="Owner" type="xs:string"
nillable="true"/>
</xs:sequence>
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:key name="PersonKey">
<xs:selector xpath=".//t:Person"/>
<xs:field xpath="@ID"/>
</xs:key>

<xs:keyref name="OwnerRef" refer="t:PersonKey">
<xs:selector xpath=".//t:Vehicle"/>
<xs:field xpath="t:Owner"/>
</xs:keyref>

</xs:element>
</xs:schema>

Jan 26 '06 #1
2 2614
Hi,

I don't think there's anything in the XML Schema rec that says to treat
nilled elements any differently than empty elements when it comes to
resolving keyrefs, so I think this is an error.

The workaround would be to omit the Owner element if it's not applicable.
Alternatively, you could insert a dummy Person element with an empty value
in the ID attribute, but that seems klugey.

Hope that helps,
Priscilla

------------------------------------------------------------------
Priscilla Walmsley
Author, Definitive XML Schema / XML in Office 2003
http://www.datypic.com
------------------------------------------------------------------

<sv*****@bigfoot.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi All!

I run into a problem defining a XSD schema with KEYREF references.

Is it possible to use KEYREF with nillable fields?
Just like a database where a FK could be null.

In the example below the tag <Owner> is nillable
and also used as FIELD in the KEYREF.

Run this in Visual Studio 2005 XML Editor and there will be NO error.
Run this in XMLSpy and there WILL BE an error.

Anyone out there with any suggestions or workarounds?

Regards
/Stefan Vestin

<== FILENAME=KeyRef.xml ==>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="testNS KeyRef.xsd" xmlns="testNS">

<Persons>
<Person ID="123">
<Name>John</Name>
</Person>
<Person ID="456">
<Name>Frank</Name>
</Person>
</Persons>

<Vehicles>
<Vehicle ID="ABC">
<Owner xsi:nil="true"></Owner> <=== Reference that is NULL
</Vehicle>
</Vehicles>

</root>
<== FILENAME=KeyRef.xsd ==>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="testNS"
targetNamespace="testNS" elementFormDefault="qualified"

attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Persons">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Vehicles">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Vehicle">
<xs:complexType>
<xs:sequence>
<xs:element name="Owner" type="xs:string"
nillable="true"/>
</xs:sequence>
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:key name="PersonKey">
<xs:selector xpath=".//t:Person"/>
<xs:field xpath="@ID"/>
</xs:key>

<xs:keyref name="OwnerRef" refer="t:PersonKey">
<xs:selector xpath=".//t:Vehicle"/>
<xs:field xpath="t:Owner"/>
</xs:keyref>

</xs:element>
</xs:schema>

Jan 27 '06 #2
Thanks for the quick answer!

<xs:element name="Owner" type="xs:string" minOccurs="0"/>

seems to be the way to go.

Regards
/Stefan

Jan 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Olaf Meyer | last post: by
reply views Thread by peterpeter | last post: by
5 posts views Thread by Harald Haspl | last post: by
2 posts views Thread by Mike | last post: by
1 post views Thread by Mike | last post: by
1 post views Thread by billa1972 | last post: by
2 posts views Thread by bmichel | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.