Question on XML schema using "restriction"

B

brucepickford001

Hi, I have a not simple problem.

I want the following XML to be schema valid

<PopulationDemographic>
<PersonName>Joe Blogs</PersonName>
<Age>2</Age>
<Category>baby</Category>
</PopulationDemographic>

And I want the following XML to be schema invalid

<PopulationDemographic>
<PersonName>Joe Blogs</PersonName>
<Age>2</Age>
<Category>teenager</Category>
</PopulationDemographic>

I can see how to create the restriction of "baby", "todler",
"teenager" and "pensioner" for my element "Category". I can see how to
restrict the ages to one of "1", "2", "3" ... "110" using similar
code. But I can't work out how to say in the schema

if "Category" = "baby" then valid ages must be "1", "2"
if "Category" = "todler" then valid ages must be "2", "3", "4" and "5"
if "Category" = "teenager" then valid ages must be "13", "14", "15",
"16", "17", "18" and "19"
if "Category" = "pensioner" then valid ages must be "65", "66" ....
"110"


Here is my schema now

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PopulationDemographic">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonName" type="xs:string"/>
<xs:element name="Age" type="ages"/>
<xs:element name="Category" type="category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="category">
<xs:restriction base="xs:string">
<xs:enumeration value="baby"/>
<xs:enumeration value="todler"/>
<xs:enumeration value="teenager"/>
<xs:enumeration value="pensioner"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ages">
<xs:restriction base="xs:string">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
<xs:enumeration value="4"/>
<!-- all the others up to 110 -->
</xs:restriction>
</xs:simpleType>
</xs:schema>

How can I do this? I have a feeling there is going to be an xpath and
keyref here somewhere but don't have the skills yet to know this for
certain or write the code (I am using xmlspy and the book xml in a
nutshell).

If anyone has an online reference, a pseudo code snippet, or help me
out with extending above for say todlers (I will do the rest) I would
really appreciate it.

This is my first post to newsgroup so sorry to have to ask a nasty
question like this without contributing anything first.
Thank you

Bruce
 
U

usenet

Hi, I have a not simple problem.

I want the following XML to be schema valid

<PopulationDemographic>
<PersonName>Joe Blogs</PersonName>
<Age>2</Age>
<Category>baby</Category>
</PopulationDemographic>

And I want the following XML to be schema invalid

<PopulationDemographic>
<PersonName>Joe Blogs</PersonName>
<Age>2</Age>
<Category>teenager</Category>
</PopulationDemographic>

I can see how to create the restriction of "baby", "todler",
"teenager" and "pensioner" for my element "Category". I can see how to
restrict the ages to one of "1", "2", "3" ... "110" using similar
code. But I can't work out how to say in the schema

if "Category" = "baby" then valid ages must be "1", "2"
if "Category" = "todler" then valid ages must be "2", "3", "4" and "5"
if "Category" = "teenager" then valid ages must be "13", "14", "15",
"16", "17", "18" and "19"
if "Category" = "pensioner" then valid ages must be "65", "66" ....
"110"

Here is my schema now

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PopulationDemographic">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonName" type="xs:string"/>
<xs:element name="Age" type="ages"/>
<xs:element name="Category" type="category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="category">
<xs:restriction base="xs:string">
<xs:enumeration value="baby"/>
<xs:enumeration value="todler"/>
<xs:enumeration value="teenager"/>
<xs:enumeration value="pensioner"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ages">
<xs:restriction base="xs:string">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
<xs:enumeration value="4"/>
<!-- all the others up to 110 -->
</xs:restriction>
</xs:simpleType>
</xs:schema>

How can I do this? I have a feeling there is going to be an xpath and
keyref here somewhere but don't have the skills yet to know this for
certain or write the code (I am using xmlspy and the book xml in a
nutshell).

If anyone has an online reference, a pseudo code snippet, or help me
out with extending above for say todlers (I will do the rest) I would
really appreciate it.

This is my first post to newsgroup so sorry to have to ask a nasty
question like this without contributing anything first.
Thank you

Bruce

Hi Bruce,

Firstly I suggest you make your 'age' type similar to

<xs:simpleType name="ages">
<xs:restriction base="xs:int">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="110"/>
</xs:restriction>
</xs:simpleType>

In terms of checking your categories are the right age range,
currently this is not possible in XSD alone. The current approach to
such problems seems to be to suggest using Schematron embedded in the
schema to specify the constraints (http://www.schematron.com/). The
current version of XSD 1.1 being developed is likely to allow such
constraints to be specified, but there is no fixed date for when it
will be released yet.

It might just be easier to check it at the application level!

HTH,

Pete.
=============================================
Pete Cordell
Tech-Know-Ware Ltd
for XML to C++ data binding visit
http://www.tech-know-ware.com/lmx
(or http://www.xml2cpp.com)
=============================================
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top