How to define an empty or type-safe element in XSD

SW

Usenet Archive
Joined
Apr 10, 2003
Messages
27
Reaction score
0
Hello,

Can anyone show me how to define in an XSD schema that an element in an XML document can either contain no text, or if it does contain text that the text has to be of a specific type.

For example, I would like both the following to be valid, where any text present should equate to an integer, but an empty element is also valid:

<mytest></mytest>

or

<mytest>123</mytest>

Many thanks in advance
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
% For example, I would like both the following to be valid, where any % text present should equate to an integer, but an empty element is also % valid:
%
% <mytest></mytest>
%
% or
%
% <mytest>123</mytest>

I was waiting to see if someone who's more of a schema person than me answered, but I'll stoke the fire by suggesting you use a union.

<xs:element name='mytest'>
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:integer"/>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,224
Members
248,132
Latest member
henrywalsh

Latest Threads

Top