簡體   English   中英

如何將限制與XSD文件中的出現指示符結合在一起?

[英]How can I combine a restriction with occurence indicators in an XSD file?

假設我有以下XSD文件:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"  elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="PACIDemoSignedDoc" type="PACIDemoSignedDocType" />
    <xs:complexType name="PACIDemoSignedDocType">
        <xs:all>
            <xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
        </xs:all>
    </xs:complexType>
    <xs:complexType name="OwnerEnglishNameType">
        <xs:simpleContent>
            <xs:restriction base="NameType">
                <xs:enumeration value="John"/>
                <xs:enumeration value="Jack"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="NameType">
        <xs:simpleContent>
            <xs:extension base="xs:string"/>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>

我的問題是,應在哪里為元素“ OwnerEnglishName”添加屬性minOccurs和maxOccurs? 我想保留xs:all,因為我想避免必須順序訂購XML文件,但是它禁止我直接在<xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />行中添加<xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" /> 。 。

我還猜測我不能在OwnerEnglishNameType內添加Occur屬性,有人知道嗎?

我的問題是,應在哪里為元素OwnerEnglishName添加屬性minOccurs和maxOccurs?

OwnerEnglishName的(非全局)聲明上:

<xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" 
            minOccurs="0" maxOccurs="1"/>

我想保留xs:all,因為我想避免必須按>順序對XML文件進行排序

在XSD 1.0中,在xs:all下不能有maxOccurs =“ unbounded”; 在XSD 1.1中,您可以。

但是,您不需要帶有單個子元素的xs:all 您可以使用xs:sequence因為沒有第二個要緊的元素。


更新 (根據示例中OP的更改,在xs:all包括其他子元素):

然后,您有三個選擇:

  1. 施加命令。 這幾乎總是最佳答案,因為在實踐中幾乎總是不需要任何排序的需求。
  2. 使用XSD 1.1,它允許xsd:all的子級具有maxOccurs="unbounded"
  3. 更改XML設計,以在xsd:all的子元素周圍使用包裝器元素,您希望允許重復該元素。 (請參閱此處的示例。)

好的,我已經解決了我的問題,如果其他人也有同樣的問題,這就是答案:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" targetNamespace="test" xmlns="test" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="animal" type="AnimalType" />
    <xs:complexType name="AnimalType">
        <xs:all>
            <xs:element name="cats" type="Cats" />
        </xs:all>
    </xs:complexType>    
    <xs:simpleType name="CatType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="John"/>
            <xs:enumeration value="Jack"/>
        </xs:restriction>
    </xs:simpleType>    
    <xs:complexType name="Cats">
        <xs:sequence>
            <xs:element maxOccurs="5" minOccurs="2" name="cat" type="Cat"/>
        </xs:sequence>
    </xs:complexType>    
    <xs:complexType name="Cat">
        <xs:simpleContent>
            <xs:extension base="CatType"/>
        </xs:simpleContent>
    </xs:complexType>    
</xs:schema>

驗證以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<animal xmlns="d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="test schema.xsd">
    <cats>
        <cat>John</cat>
        <cat>Jack</cat>
    </cats>
</animal>

通過在xs:sequence之后添加子元素,我成功繞過了xs:all的出現限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM