restricting data types

Ranjit

Usenet Archive
Joined
Mar 23, 2002
Messages
81
Reaction score
0
Hi,

This may be quite a dumb question but I have not been able to address this convincibly. How can I specify restrictions on basic data types in my xml?

I did some search and came across Schema Component Constraints such as minInclusive, maxExclusive etc.

But I was wondering if I could refer to them in my xml. e.g. <RAM> <minInclusive>512</minInclusive> </RAM>
-OR-
<cost maxExclusive = "1000"/>

or something similar.

Can this be done?

TIA
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Ranjit said:
This may be quite a dumb question but I have not been able to address this convincibly. How can I specify restrictions on basic data types in my xml?

I did some search and came across Schema Component Constraints such as minInclusive, maxExclusive etc.

But I was wondering if I could refer to them in my xml. e.g. <RAM> <minInclusive>512</minInclusive> </RAM>
-OR-
<cost maxExclusive = "1000"/>

or something similar.

Can this be done?

You can write an XML schema that defines the structure and restrictions for your XML instance files, check out http://www.w3.org/TR/xmlschema-0/

For instance you could define a schema as follows

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="ram" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="ram">
<xs:simpleType>
<xs:restriction base="xs:positiveInteger">
<xs:minInclusive value="512" />
<xs:maxInclusive value="1024" />
</xs:restriction>
</xs:simpleType>
</xs:element>

</xs:schema>

and then a validating XML parser would flag the last <ram> element in the following XML instance file as having a wrong value:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test20040411Xsd.xml"> <ram>512</ram> <ram>768</ram> <ram>1024</ram> <ram>256</ram> </root>
 

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,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top