Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an alternative to <assert> in XML Schema 1.0

I have a XSD document fragment below. When an XML file validates against this schema I wish to ensure that the value in mm:Depot and mm:Customer/mm:County are the same and restricted to items in the Location type.

This can be done in XML Schema 1.1 using the <assert> tag but I MUST validate against XML Schema 1.0. Is there some neat trick to make this work in 1.0?

<xsd:element name="DeliveryOrder" type="OrderDetails" />

<xsd:complexType name="OrderDetails">
    <xsd:sequence>
        <xsd:element name="Depot" type="mm:Location" />
        <xsd:element name="Customer" type="mm:Customer" maxOccurs="1" minOccurs="1" />
    </xsd:sequence> 
</xsd:complexType>

<xsd:simpleType name="Location">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="OverHere" />
        <xsd:enumeration value="OverThere" />
    </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="Customer">   
    <xsd:sequence>
        <xsd:element name="firstname" type="string" />
        <xsd:element name="surname" type="string" />
        <xsd:element name="County" type="mm:Location" />
    </xsd:sequence>
</xsd:complexType>

What I want to achieve is something like this...

<?xml version="1.0" encoding="UTF-8"?>
<mm:DeliveryOrder xmlns:mm="http://myNamespace/DeliveryOrderSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://myNamespace/DeliveryOrderSchema DeliveryOrder.xsd ">

  <mm:Depot>OverThere</mm:Depot>
  <mm:Customer>
    <mm:firstname>Jane</mm:firstname>
    <mm:surname>Doe</mm:surname>
    <mm:County>OverThere</mm:County>
  </mm:Customer>
</mm:DeliveryOrder>

Note the value OverThere appears twice.

like image 415
cymorg Avatar asked Oct 19 '13 17:10

cymorg


1 Answers

You could use "referential integrity".

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://myNamespace/DeliveryOrderSchema" xmlns="http://myNamespace/DeliveryOrderSchema" xmlns:mm="http://myNamespace/DeliveryOrderSchema" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="DeliveryOrder" type="OrderDetails">
        <xsd:key name="LocationDepot">
            <xsd:selector xpath="mm:Depot"/>
            <xsd:field xpath="."/>
        </xsd:key>
        <xsd:keyref refer="LocationDepot" name="CustomerLocation">
            <xsd:selector xpath="mm:Customer"/>
            <xsd:field xpath="mm:County"/>
        </xsd:keyref>
    </xsd:element>

    <xsd:complexType name="OrderDetails">
        <xsd:sequence>
            <xsd:element name="Depot" type="Location"/>
            <xsd:element name="Customer" type="Customer" maxOccurs="1" minOccurs="1"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:simpleType name="Location">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="OverHere"/>
            <xsd:enumeration value="OverThere"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:complexType name="Customer">
        <xsd:sequence>
            <xsd:element name="firstname" type="xsd:string"/>
            <xsd:element name="surname" type="xsd:string"/>
            <xsd:element name="County" type="Location"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

An error might look as below:

Error occurred while loading [], line 10 position 3 The key sequence 'OverHere' in 'http://myNamespace/DeliveryOrderSchema:LocationDepot' Keyref fails to refer to some key.

like image 79
Petru Gardea Avatar answered Oct 15 '22 00:10

Petru Gardea