在XML Schema 1.0 </assert>中是否有<assert>的替代方法

时间:2013-10-19 17:19:22

标签: xml xsd assert xsd-validation xsd-1.0

我在下面有一个XSD文档片段。当XML文件针对此架构进行验证时,我希望确保mm:Depotmm:Customer/mm:County中的值相同并且仅限于Location类型中的项。

这可以使用<assert>标记在XML Schema 1.1中完成,但我必须针对XML Schema 1.0进行验证。是否有一些巧妙的技巧使这项工作在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>

我想要实现的是这样......

<?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>

请注意,值OverThere出现两次。

1 个答案:

答案 0 :(得分:1)

您可以使用“参照完整性”。

<?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>

错误可能如下所示:

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