开发者

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

开发者 https://www.devze.com 2023-01-04 07:32 出处:网络
I need to define a XML schema. The result should look like this: <option value=\"priority\">4</option>

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 开发者_JS百科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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号