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

S

SW

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
 
P

Patrick TJ McPhee

% 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
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top