3

少なくとも1つを選択するように強制するにはどうすればよいですか?

次の構文では、任意の c 要素を 3 回まで繰り返すことができます。

<choice minOccurs="1" maxOccurs="3">
    <element name="c1" type="string" />
    <element name="c2" type="string" />             
    <element name="c3" type="string" />
</choice>

thnx

スティーブ

4

1 に答える 1

3

を緩めるmaxOccurs="3"と、「少なくとも 1 つ選択する」だけで、繰り返しはありません。

パーティクルの場合、デフォルトはminOccurs="1"; 各オプション粒子自体が必須である必須の選択があなたの答えです。

更新:あなたのコメントに基づいて、あなたが探しているのが、説明した粒子の順序付けられた組み合わせの場合、これは XSD 仕様で取得できる最高のものです。

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:choice>
                <xsd:sequence>
                    <xsd:element ref="c1"/>
                    <xsd:element ref="c2" minOccurs="0"/>
                    <xsd:element ref="c3" minOccurs="0"/>
                </xsd:sequence>
                <xsd:sequence>
                    <xsd:element ref="c2"/>
                    <xsd:element ref="c3" minOccurs="0"/>
                </xsd:sequence>
                <xsd:element ref="c3"/>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="c1" type="xsd:string"/>
    <xsd:element name="c2" type="xsd:string"/>
    <xsd:element name="c3" type="xsd:string"/>
</xsd:schema>

これはすでに厄介です。より多くの粒子または任意の順序付けられていない組み合わせを探している場合は、モデルを次のように変更します (これらは実際の XSD 1.0 の制限です。これはすべて、使用できる XPath 構文の制限に関係しています。セレクター/フィールド)。

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="c" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:key name="pk">
            <xsd:selector xpath="*"/>
            <xsd:field xpath="@code"/>
        </xsd:key> 
    </xsd:element>
    <xsd:element name="c" type="TC" abstract="true"/>   
    <xsd:element name="c1" type="TC1" substitutionGroup="c"/>
    <xsd:element name="c2" type="TC2" substitutionGroup="c"/>
    <xsd:element name="c3" type="TC3" substitutionGroup="c"/>

    <xsd:complexType name="TC">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:attribute name="code" type="xsd:string"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
    <xsd:complexType name="TC1">
        <xsd:simpleContent>
            <xsd:restriction base="TC">
                <xsd:attribute name="code" type="xsd:string" fixed="c1"/>
            </xsd:restriction>
        </xsd:simpleContent>
    </xsd:complexType>
    <xsd:complexType name="TC2">
        <xsd:simpleContent>
            <xsd:restriction base="TC">
                <xsd:attribute name="code" type="xsd:string" fixed="c2"/>
            </xsd:restriction>
        </xsd:simpleContent>
    </xsd:complexType>
    <xsd:complexType name="TC3">
        <xsd:simpleContent>
            <xsd:restriction base="TC">
                <xsd:attribute name="code" type="xsd:string" fixed="c3"/>
            </xsd:restriction>
        </xsd:simpleContent>
    </xsd:complexType>  
</xsd:schema>

サンプル XML は次のようになります。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <c1 code="c1">c11</c1>
    <c2 code="c2">c21</c2>
    <c3 code="c3">c21</c3>
</root>

またはこれ:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <c2 code="c2">c21</c2>
    <c1 code="c1">c11</c1>
</root>

基本的に、タグ名ではなくデータの一部である、要素を一意にするコンポーネントをキー入力しています。繰り返しますが、面倒ですが、演習として、アイデアが得られるかもしれません。

于 2012-04-27T21:12:44.683 に答える