简体   繁体   中英

XML Schema - how to define an element that contains a string or child elements

I need to define a XML schema. The result should look like this:

<option value="priority">4</option>
<option value="values">
    <value name="x86-32" lang="en-GB" group="root">x86 32-Bit</value>
    <value name="x86-64" lang="en-GB" group="root">x86 64-Bit</value>
    <value name="ARM" lang="en-GB" group="root">ARM</value>
    <value name="PowerPC" lang="en-GB" group="root">PowerPC</value>
    <value name="SPARC" lang="en-GB" group="root">SPARC</value>
    <value name="PA-RISC" lang="en-GB" group="root">PA-RISC</value>
    <value name="DEC-Alpha" lang="en-GB" group="root">DEC Alpha</value>
</option>
<option value="editable">true</option>

So the element "option" contains either a string or set of child elements with strings. I have tried something like this:

<xs:element name="options" minOccurs="0"  maxOccurs="1">
    <xs:complexType mixed="true">
        <xs:sequence>
            <xs:element name="option" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">                              
                            <xs:attribute name="value" use="required" type="xs:string" />
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>                       

But the validator doesn't allow this definition:

cvc-complex-type.2.2: Element 'option' must have no element [children], and the value must be valid.

Any idea how to solve it?

Best regards, Radek

Ok, I think I've found acceptable (for me) solution:

<xs:element name="options" minOccurs="0"  maxOccurs="1">
<xs:complexType>
    <xs:choice maxOccurs="unbounded">                                                                       
        <xs:element name="option" maxOccurs="unbounded">
              <xs:complexType>
                <xs:simpleContent>
                    <xs:extension base="xs:string">                              
                        <xs:attribute name="attribute" use="required" type="xs:string" />
                    </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
        </xs:element>
        <xs:element name="values" maxOccurs="unbounded">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="value" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:simpleContent> 
                                <xs:extension base="xs:string"> 
                                    <xs:anyAttribute/>
                                </xs:extension>
                            </xs:simpleContent>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="attribute" use="required" type="xs:string" />
              </xs:complexType>
        </xs:element>                                                                       
    </xs:choice>
</xs:complexType>

This allows me to create such XML file:

<option attribute="priority">4</option>
<values attribute="fieldOptions">
    <value name="x86-32" lang="en-GB" group="root">x86 32-Bit</value>
    <value name="x86-64" lang="en-GB" group="root">x86 64-Bit</value>
    <value name="ARM" lang="en-GB" group="root">ARM</value>
    <value name="PowerPC" lang="en-GB" group="root">PowerPC</value>
    <value name="SPARC" lang="en-GB" group="root">SPARC</value>
    <value name="PA-RISC" lang="en-GB" group="root">PA-RISC</value>
    <value name="DEC-Alpha" lang="en-GB" group="root">DEC Alpha</value>
</values>
<option attribute="editable">true</option>

Thanks to AllenG for the suggestion :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM