0

I'm falling over on a frustrating, arbitrary restriction in XML Schema. For some reason, it insists that PK-FK relationships have to be one-to-one. Why?

For example, given the schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="FutureSchema"
    targetNamespace="http://tempuri.org/FutureSchema.xsd"
    elementFormDefault="unqualified"
    xmlns="http://tempuri.org/FutureSchena.xsd"
    xmlns:mstns="http://tempuri.org/FutureSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="FUTUREFILE">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Configuration">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Experiments">

                <xs:complexType>
                  <xs:sequence>

                    <xs:element name="Experiment">

                      <xs:complexType>
                        <xs:attribute name="ID" type="xs:integer"/>
                        <xs:attribute name="Profile" type="xs:integer"/>
                      </xs:complexType>

                      <xs:keyref name="dummy" refer="LP">
                        <xs:selector xpath="Experiment"/>
                        <xs:field xpath="@Profile"/>
                      </xs:keyref>

                    </xs:element>

                  </xs:sequence>
                </xs:complexType>

                <xs:key name="LP">
                  <xs:selector xpath="*/Configuration/Profiles/Profile"/>
                  <xs:field xpath="@ID"/>
                </xs:key>

              </xs:element>
              <xs:element name="Profiles">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Profile">
                      <xs:complexType>
                        <xs:attribute name="ID" type="xs:integer"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

And an example instance:

<?xml version="1.0" encoding="utf-8" ?>
<b:FUTUREFILE xmlns:b="http://tempuri.org/FutureSchena.xsd">
  <Configuration>
    <Experiments>
      <Experiment ID="1" Profile="1"/>
      <Experiment ID="2" Profile="1"/>
    </Experiments>
    <Profiles>
      <Profile ID="1"/>
    </Profiles>
  </Configuration>
</b:FUTUREFILE>

Document validation throws an error if I define <Experiment ID="1" FK="1"/><Experiment ID="2" FK="1"/>, for example; i.e. more than one Experiment may not reference the same Profile. But why else would I want to use a key relationship? What use is a key relationship at all if I can't do something so fundamental?

OK, if and won't let me do this, how should I?

edit #1: As requested, I've padded out my code sample to include the full schema and a basic instance.

edit #2: Interesting. SharpDevelop's XML editor (as opposed to Visual Studio's) doesn't seem to object. It doesn't object to the foreign key value referring to a nonexistent primary key either (which IMO it should) but it's a start.

Tom W
  • 5,108
  • 4
  • 30
  • 52
  • Please provide a complete short schema and instance, so I can do some experiments by myself. Thanks. – Istao Dec 18 '10 at 18:09
  • Done. Out of curiosity, what wasn't clear about how this was supposed to work, given my previous (incomplete) example? I felt it was enough to cover all the bases. – Tom W Dec 19 '10 at 11:31

1 Answers1

2

What wasn't clear ?... My english, it's all :-)

Some remarqs, I'm not sure it's solution :

In exemple instance, it's best to use xmlns:b="http://tempuri.org/FutureSchema.xsd">, and not xmlns:b="http://tempuri.org/FutureSchena.xsd"> (n -> m).

Also it's best to put b: in front of all elements names.

According to your schema, you can't have two Experiment in Experiments, only one.

On xsd, put <xs:keyref name="dummy" refer="mstns:LP">, that works best for me ; it's because xpath expression doesnt't understand default namespace, see Correct way to use key in xsd.

Community
  • 1
  • 1
Istao
  • 7,425
  • 6
  • 32
  • 39
  • My apologies; my schema was a copied version of one I wrote at work, therefore I didn't get it exactly right. That version definitely did permit multiple Experiments with *different* profiles. Ditto with the spelling mistake in *schema*. – Tom W Dec 19 '10 at 16:21
  • There we go. This led me to the correct answer - it seemed not to be applying the key constraint correctly because of the default namespace problem. Furthermore, having rewritten the same logic **again**, the .net xml DOM now accepts one-to-many PK-FK correctly. What was wrong the first time, I genuinely cannot tell. Still, it works now. Many thanks. – Tom W Dec 20 '10 at 13:00