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

1

7

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.

Valera answered 19/10, 2013 at 17:19 Comment(0)
O
2

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.

Osric answered 19/10, 2013 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.