all groups > dotnet xml > january 2006 >
You're in the

dotnet xml

group:

XSD Schema - nillable fields in keyref


XSD Schema - nillable fields in keyref svestin NO[at]SPAM bigfoot.com
1/26/2006 7:09:18 AM
dotnet xml: 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>
Re: XSD Schema - nillable fields in keyref Priscilla Walmsley
1/26/2006 6:57:40 PM
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
------------------------------------------------------------------

[quoted text, click to view]

Re: XSD Schema - nillable fields in keyref svestin NO[at]SPAM bigfoot.com
1/27/2006 2:07:47 AM
Thanks for the quick answer!

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

seems to be the way to go.

Regards
/Stefan
AddThis Social Bookmark Button