Avatar of tomaugerdotcom
tomaugerdotcom
Flag for Canada

asked on 

Foreign Key in XML Schema

Hi, I'm running into a snag implementing foreign keys in my XML schema.

Here's a bare-bones version of my XML:

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

<test

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="test.xsd">


      <shell_styles>
            <shell code="MX01">Test 1</shell>
            <shell code="MO02">Test 2</shell>
            
            <!--
            <shell code="MO02">Invalid Duplicate</shell>
            -->
      </shell_styles>
            

      <!-- If validating against the schema, this document should throw an error if the brand definition references a shell_style that has not already been defined in this document -->
      <brands>

            <brand name="Test">
                  <shell_style code="MX01">
                        <colour_code>01</colour_code>
                  </shell_style>
                  <shell_style code="MO02">
                        <colour_code>01</colour_code>
                  </shell_style>
                  
                  <!-- Invalid foreign key -->
                  <shell_style code="ZZ99">
                        <colour_code>00</colour_code>
                  </shell_style>
            </brand>
      </brands>
      
            
</test>


//------------------------------------------

As you can see from this XML file, what I'm attempting to do is make it so that each brands/brand/shell_style item's "code" attribute points toward a valid shell_styles/shell "code" attribute.

It appears that my PK for shell_styles/shell is working fine. If I uncomment the invalid duplicate shell item, my validator throws what I expect the correct error to be.

However, it's not my invalid foreign key brand/shell_style code that's causing the validator to carp - it's actually the very first code "MX01" that for some reason is not able to find any corresponding key.

Even the error that the validator throws is a little odd:
Key 'shell_style_id' with value 'MX01' not found for identity constraint of element 'test'.

Anyway, I'm attaching the schema. I'd really appreciate someone with a little more experience wading through and pointing out what I've missed.

Many thanks in advance, experts!

Tom
<?xml version="1.0" encoding="UTF-8"?>
 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="test">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="shell_styles" />
				<xs:element ref="brands" />
			</xs:sequence>
		</xs:complexType>
		
		<!-- Primary Key -->
		<xs:key name="shell_id">
			<xs:selector xpath="./shell_styles/shell"></xs:selector>
			<xs:field xpath="@code"></xs:field>
		</xs:key>			
		
		<!-- Foreign Keys -->
		<!-- Brand: shell_style code -->
        <xs:keyref name="shell_style_id" refer="shell_id" >
            <xs:selector xpath="./brands/brand/shell_style"></xs:selector>
            <xs:field xpath="@code"></xs:field>
        </xs:keyref>
		
	</xs:element>
	
 
	<xs:element name="shell_styles">
		<xs:complexType>
			<xs:sequence>
				<!-- Shell name and code -->
				<xs:element name="shell" maxOccurs="unbounded">
					<xs:complexType>
						<xs:simpleContent>
							<xs:extension base="xs:string">
								<xs:attribute name="code" use="required">
									<xs:simpleType><xs:restriction base="xs:string"><xs:pattern value="[A-Z]{2}[0-9]{2}"/></xs:restriction></xs:simpleType>
								</xs:attribute>
							</xs:extension>
						</xs:simpleContent>
					</xs:complexType>
				</xs:element> 
			</xs:sequence>
		</xs:complexType>
 
		<!-- Primary key -->
			
	</xs:element>
 
	
	
	<!--	Brands: by brand, a list of valid shell styles, and for each shell style, a list of valid colour codes -->
	<xs:element name="brands">
		<xs:complexType>
			<xs:sequence>
				<!-- Brand matrix -->
				<xs:element name="brand" maxOccurs="unbounded">
					<xs:complexType>
						<xs:sequence>
							<!-- Shell style collection -->
							<xs:element name="shell_style" maxOccurs="unbounded">
								<xs:complexType>
									<!-- Colour code -->
									<xs:sequence><xs:element name="colour_code" maxOccurs="unbounded" type="xs:string" /></xs:sequence>
									<xs:attribute name="code" use="required" />
								</xs:complexType>
							</xs:element>
						</xs:sequence>
						<!-- Brand name -->
						<xs:attribute name="name" use="required" />
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
 
 
</xs:schema>

Open in new window

.NET ProgrammingXML

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon