6

我在下面有一个 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出现两次。

4

1 回答 1

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.

于 2013-10-19T21:35:47.597 回答