xmlxsdxmlschemaset

Relational XML Schema isn't working perfectly


I've been working on a xsd file based on a pretty easy-to-understand database tables. Just imagine "users". Those users have normal field (firstname, lastname, etc.) and also have a phone and an address field (Each user can have more than one phone and address). And I wanted to create the best XML Schema for this kind of relation(s) for my future purpose of generating xsd files based on predetermined Schemas.

So I got this for my XML Schema (But the important part I think is where a implement my keys and keyrefs) :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
  elementFormDefault="qualified"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

  <xs:element name="Root" msdata:IsDataSet="true">
    <xs:annotation>
      <xs:documentation>Contains All Object</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="User" maxOccurs="unbounded">
        </xs:element>
        <xs:element ref="UserPhone" maxOccurs="unbounded" />
        <xs:element ref="UserAddress" maxOccurs="unbounded" />
        <xs:element ref="AddressLine" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
    <xs:key name="UserUID">
      <xs:selector xpath="User" />
      <xs:field xpath="UserUID" />
    </xs:key>
    <xs:key name="PhoneUID">
      <xs:selector xpath="UserPhone" />
      <xs:field xpath="PhoneUID" />
    </xs:key>
    <xs:key name="AddressUID">
      <xs:selector xpath="UserAddress" />
      <xs:field xpath="AddressUID" />
    </xs:key>
    <xs:key name="AddressLineUID">
      <xs:selector xpath="AddressLine" />
      <xs:field xpath="AddressLineUID" />
    </xs:key>
    <xs:keyref name="FKPhonetoUsers" refer="PhoneUID">
      <xs:selector xpath="User/UserPhones/*" />
      <xs:field xpath="PhoneUID" />
    </xs:keyref>
    <xs:keyref name="FKAddresstoUsers" refer="AddressUID">
      <xs:selector xpath="User/UserAddresses/*" />
      <xs:field xpath="AddressUID" />
    </xs:keyref>
    <xs:keyref name="FKAddressLinetoAddresses" refer="AddressLineUID">
      <xs:selector xpath="UserAddress/AddressLines/*" />
      <xs:field xpath="AddressLineUID" />
    </xs:keyref>
  </xs:element>

  <xs:element name="User">
    <xs:annotation>
      <xs:documentation>Contains All Employee Information</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="UserUID" type="xs:string" />
        <xs:element name="UserFirstName" type="xs:string" />
        <xs:element name="UserLastName" type="xs:string" />
        <xs:element name="UserIDLogin" type="IDLoginPatern" />
        <xs:element name="UserEmail" type="EmailPatern" />
        <xs:element name="UserPhones">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="PhoneUID" maxOccurs="3" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="UserDepartment" type="xs:nonNegativeInteger" default="0" />
        <xs:element name="UserSector" type="xs:string" />
        <xs:element name="UserTitle" type="TitleEnum" />
        <xs:element name="UserAddresses">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="AddressUID" maxOccurs="2" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="UserOfSystem" type="xs:boolean" />
        <xs:element name="UserRole" type="xs:string" />
        <xs:element name="UserPW" type="PWordPatern" />
        <xs:element name="UserPWFailCount" type="xs:nonNegativeInteger" default="0"/>
        <xs:element name="UserPWFailDate" type="xs:date" minOccurs="0" />
        <xs:element name="UserProfileCreated" type="xs:date" />
        <xs:element name="UserLastAccessSystem" type="xs:date" minOccurs="0" />
        <xs:element name="UserPic" type="xs:anyURI" minOccurs="0" />
        <xs:element name="SSMA_TimeStamp" type="xs:base64Binary" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="UserPhone">
    <xs:annotation>
      <xs:documentation>Contains All Phone Created</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="PhoneUID" type="xs:string" />
        <xs:element name="type" type="TelephoneTypeEnum" />
        <xs:element name="number" type="TelephonePatern" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="UserAddress">
    <xs:annotation>
      <xs:documentation>Contains All Address Created</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="AddressUID" type="xs:string" />
        <xs:element name="AddressLines">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="AddressLineUID" maxOccurs="2" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="state" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
        <xs:element name="postalCode" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="AddressLine">
    <xs:annotation>
      <xs:documentation>Contains All Address Line Completing the Address Objects Created</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="AddressLineUID" type="xs:string" />
        <xs:element name="streetLine" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!-- All Telephone patern (123) 456-7890 -->
  <xs:simpleType name="TelephonePatern">
    <xs:restriction base="xs:string">
      <xs:pattern value="\([0-9]{3}\) [0-9]{3}-[0-9]{4}"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- All ID Login patern (LOGINONE) -->
  <xs:simpleType name="IDLoginPatern">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]+"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- All Email patern (blabla@gmail.com) -->
  <xs:simpleType name="EmailPatern">
    <xs:restriction base="xs:string">
      <xs:pattern value="[^@]+@[^\.]+\..+"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- All Password patern (aaaaa) -->
  <xs:simpleType name="PWordPatern">
    <xs:restriction  base="xs:string">
      <xs:minLength  value="4" />
      <xs:maxLength  value="30" />
      <xs:pattern  value="[a-zA-Z0-9\p{P}$\^\+=~`|]+" />
    </xs:restriction>
  </xs:simpleType>

  <!-- Person title enumeration -->
  <xs:simpleType name="TitleEnum" final="restriction" >
    <xs:restriction base="xs:string">
      <xs:enumeration value="Mr." />
      <xs:enumeration value="Ms." />
      <xs:enumeration value="Miss" />
    </xs:restriction>
  </xs:simpleType>

  <!-- Telephone type enumeration -->
  <xs:simpleType name="TelephoneTypeEnum" final="restriction" >
    <xs:restriction base="xs:string">
      <xs:enumeration value="Work" />
      <xs:enumeration value="Home" />
      <xs:enumeration value="Fax" />
      <xs:enumeration value="Personal" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

After that, I've created a little XML file to test if all my keys, format, patterns and enumerations are working well.

This is what I got so far :

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <User>
    <UserUID>UserUID1</UserUID>
    <UserFirstName>UserFirstName1</UserFirstName>
    <UserLastName>UserLastName1</UserLastName>
    <UserIDLogin>USERONE</UserIDLogin>
    <UserEmail>userone@gmail.com</UserEmail>
    <UserPhones>
      <PhoneUID>PhoneUID1</PhoneUID>
      <PhoneUID>PhoneUID2</PhoneUID>
      <PhoneUID>PhoneUID4</PhoneUID>
    </UserPhones>
    <UserDepartment>0</UserDepartment>
    <UserSector>UserSector1</UserSector>
    <UserTitle>Mr.</UserTitle>
    <UserAddresses>
      <AddressUID>AddressUID1</AddressUID>
      <AddressUID>AddressUID2</AddressUID>
    </UserAddresses>
    <UserOfSystem>true</UserOfSystem>
    <UserRole>UserRole1</UserRole>
    <UserPW>UserPW1</UserPW>
    <UserPWFailCount>0</UserPWFailCount>
    <UserPWFailDate>1900-01-01</UserPWFailDate>
    <UserProfileCreated>1900-01-01</UserProfileCreated>
    <UserLastAccessSystem>1900-01-01</UserLastAccessSystem>
    <UserPic>http://uri1</UserPic>
    <SSMA_TimeStamp>AAECAwQFBgcICQoLDA0ODw==</SSMA_TimeStamp>
  </User>
  <UserPhone>
    <PhoneUID>PhoneUID1</PhoneUID>
    <type>Work</type>
    <number>(613) 333-4455</number>
  </UserPhone>
  <UserPhone>
    <PhoneUID>PhoneUID2</PhoneUID>
    <type>Home</type>
    <number>(613) 444-5566</number>
  </UserPhone>
  <UserPhone>
    <PhoneUID>PhoneUID3</PhoneUID>
    <type>Fax</type>
    <number>(613) 555-6677</number>
  </UserPhone>
  <UserAddress>
    <AddressUID>AddressUID1</AddressUID>
    <AddressLines>
      <AddressLineUID>AddressLineUID1</AddressLineUID>
      <AddressLineUID>AddressLineUID2</AddressLineUID>
    </AddressLines>
    <city>city1</city>
    <state>state1</state>
    <country>country1</country>
    <postalCode>postalCode1</postalCode>
  </UserAddress>
  <UserAddress>
    <AddressUID>AddressUID2</AddressUID>
    <AddressLines>
      <AddressLineUID>AddressLineUID3</AddressLineUID>
      <AddressLineUID>AddressLineUID4</AddressLineUID>
    </AddressLines>
    <city>city2</city>
    <state>state2</state>
    <country>country2</country>
    <postalCode>postalCode2</postalCode>
  </UserAddress>
  <AddressLine>
    <AddressLineUID>AddressLineUID2</AddressLineUID>
    <streetLine>streetLine2</streetLine>
  </AddressLine>
  <AddressLine>
    <AddressLineUID>AddressLineUID3</AddressLineUID>
    <streetLine>streetLine3</streetLine>
  </AddressLine>
</Root>

Iv'e tested both file together and it worked well but I don't think it's supposed to work. I don't understand what I've been doing wrong. Because, since I have references between my keys why does the line :

<PhoneUID>PhoneUID4</PhoneUID>

In my XML file is working fine ? I did'nt create any "UserPhone" with the id : PhoneUID4.

I've tested my files there : http://www.utilities-online.info/xsdvalidation/#.WnIPJq6nGUk

Can you help me on this ?

Thanks a lot, Vincent


Solution

  • You define the keyref as

    <xs:keyref name="FKPhonetoUsers" refer="PhoneUID">
        <xs:selector xpath="User/UserPhones/*" />
        <xs:field xpath="PhoneUID" />
    </xs:keyref>
    

    Now, User/UserPhones/* selects three PhoneUID elements. The xs:field expression is evaluated relative to these elements. None of these has a child called PhoneUID, so nothing is selected, so nothing is validated. Change the xs:field to xpath="." and it works fine.

    You're obviously deeply steeped in SQL thinking, and one of the common mistakes SQL users make with XPath is to forget that using an element name X means child::X rather than self::X.