user1139216
user1139216

Reputation: 119

XSD key with Namespace

I have an issue that i can't fix by my own. My XSD is very complex but here it is a simpler code, which have the same issue.

This is the XSD :

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema id="Race"
           xmlns:xs = "http://www.w3.org/2001/XMLSchema"
           targetNamespace="myrace.org/"
           xmlns="myrace.org/"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

  <xs:element name="race" msdata:IsDataSet="true">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="runner" type="runnerType" maxOccurs="10">
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:keyref name="predecessorKey" refer="runKey">
      <xs:selector xpath="runner/predecessor"/>
      <xs:field xpath="@foreignKey"/>
    </xs:keyref>
    <xs:unique name="runIdChecker">
      <xs:selector xpath="runner"/>
      <xs:field xpath="@primaryKey"/>
    </xs:unique>
    <xs:unique name="oneToOneChecker">
      <xs:selector xpath="runner/predecessor"/>
      <xs:field xpath="@foreignKey"/>
    </xs:unique>
    <xs:key name="runKey">
      <xs:selector xpath="runner"/>
      <xs:field xpath="@primaryKey"/>
    </xs:key>
  </xs:element>

  <xs:complexType name="runnerType">
    <xs:sequence>
      <xs:element name="firstName" type="xs:string"/>
      <xs:element name="lastName" type="xs:string"/>
      <xs:element name="predecessor" type="predecessorType" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="primaryKey" type="xs:long" use="required"/>
  </xs:complexType>

  <xs:complexType name="predecessorType">
    <xs:attribute name="foreignKey" type="xs:long" use="required"/>
  </xs:complexType>

</xs:schema>

The XSD work normally but it should generate an error when we write 2 similar primaryKey, and it's not.

In the other hand if I change the head to:

<xs:schema id="Race"
           xmlns:xs = "http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

It will work. But for a reason of concistancy with my global project I can't change the head and need to have a definition of the connection between XSD -> XML equivalent to the first code.

Does anyone have any idea of what is my problem and what i can do?

Thanks

Benjamin

Upvotes: 2

Views: 1589

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21658

Take a look at this answer, particularly the part that speaks about the need to use namespace prefixes for selectors and fields when your schema targets a namespace.

So, all you need to do is add xmlns:my="myrace.org/" and then you change your selectors to read something like my:runner/my:predecessor - it'll work. Attributes are unqualified, so you don't have to worry about them.

I haven't visualized your constraints for correctness, but if you run into issues, update the post.

Upvotes: 3

Related Questions