42

次の XSD コードがあります。

<xsd:complexType name="questions">
    <xsd:sequence>
        <xsd:element name="location" type="location"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="pictureInput" type="pictureInput" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

ここでの問題は、要素の場所、multipleChoiceInput などは、宣言されているのと同じ順序で表示する必要があることです。私はこれが起こってほしくありません。検証プロセスでは、シーケンスが関連してはなりません。どうすればこれを達成できますか?

私が試した別の可能性は次のとおりです。

<xsd:complexType name="questions">

        <xsd:choice maxOccurs="unbounded">   
            <xsd:element name="location" type="location"/>  
            <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
        </xsd:choice>            

</xsd:complexType>

この例では、シーケンスはもはや重要ではなく、必要な数の要素を使用できます (「すべて」ではできないこと)。しかし、minOccurs と maxOccurs にはまだ問題があります。この例では、できるだけ多くの「pictureInput」を使用できますが、0 または 1 のいずれかを使用したいという制約があります。

助けてくれてありがとう!

4

3 に答える 3

54
<xsd:complexType name="questions">
    <xsd:all>
        <xsd:element name="location" type="location"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
        <xsd:element name="textInput" type="textInput"/>
        <xsd:element name="pictureInput" type="pictureInput"/>
    </xsd:all>
</xsd:complexType>

注:「シーケンス」を「すべて」に変更しました

Sequence は順序を強制します (定義どおり)。順序が重要でない場合は、すべてが使用されます。

要素が複数回出現する可能性がある場合は、xsd:any を使用できます。

<xsd:complexType name="questions">
    <xsd:sequence>
        <xsd:any minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

次のリンクで xsd:any の詳細を確認できます。

https://www.w3schools.com/xml/schema_complex_any.asp

于 2010-07-24T14:09:06.137 に答える
20

私はこの議論に少し遅れていますが、同じ問題があり、解決策を見つけました:

<xsd:complexType name="questions">
    <xsd:choice maxOccurs="unbounded">
        <xsd:element name="location" type="location"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
        <xsd:element name="textInput" type="textInput"/>
        <xsd:element name="pictureInput" type="pictureInput"/>
    </xsd:choice>
</xsd:complexType>

重要なのは、xs:choice と maxOccurs="unbounded" を組み合わせることです。xs:all のみを使用する場合は、それぞれのピリオドを 1 つ使用できます。

追加するために編集: xs:any は機能しますが、選択肢を項目化された 4 つの要素に限定しません。スキーマの目的をほとんど無効にするものは何でも許可します。

于 2011-12-23T16:56:53.297 に答える
1

ここのパーティーにも非常に遅れていますが、<xsd:all>一緒に使用すると機能minOccursmaxOccursませんか?:

<xsd:complexType name="questions">
    <xsd:all>
        <xsd:element name="location" type="location" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
    </xsd:all>
</xsd:complexType>
于 2015-05-26T11:11:54.907 に答える