Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

xsd - XML Schema: Element that can contain elements or text?

How would I define an element that can either contain plain text or contain elements? Say I wanted to somehow allow for both of these cases:

<xs:element name="field">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="subfield" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" />
    </xs:complexType>
</xs:element>

<xs:element name="field" type="xs:string" />

... so that both these elements would be valid:

<field name="test_field_0">
    <subfield>Some text.</subfield>
</field>

<field name="test_field_1">Some more text.</field>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did some research on this a while ago and the only solution I found was to used the mixed attribute:

<xs:element name="field">
    <xs:complexType mixed="true">
        <xs:sequence>
                <xs:element ref="subfield" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" />
    </xs:complexType>
</xs:element>

This sadly also allows

<field name="test_field_0">
    Some text I'm sure you don't want.
    <subfield>Some text.</subfield>
    More text you don't want.
</field>

Hopefully someone will give a better answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...