473,785 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on XML schema using "restrictio n"


Hi, I have a not simple problem.

I want the following XML to be schema valid

<PopulationDemo graphic>
<PersonName>J oe Blogs</PersonName>
<Age>2</Age>
<Category>bab y</Category>
</PopulationDemog raphic>

And I want the following XML to be schema invalid

<PopulationDemo graphic>
<PersonName>J oe Blogs</PersonName>
<Age>2</Age>
<Category>teena ger</Category>
</PopulationDemog raphic>

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="Populatio nDemographic">
<xs:complexType >
<xs:sequence>
<xs:element name="PersonNam e" type="xs:string "/>
<xs:element name="Age" type="ages"/>
<xs:element name="Category" type="category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleTyp e name="category" >
<xs:restricti on base="xs:string ">
<xs:enumerati on value="baby"/>
<xs:enumerati on value="todler"/>
<xs:enumerati on value="teenager "/>
<xs:enumerati on value="pensione r"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleTyp e name="ages">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="1"/>
<xs:enumerati on value="2"/>
<xs:enumerati on value="3"/>
<xs:enumerati on 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

Feb 16 '07 #1
1 1943
On 16 Feb, 15:23, brucepickford.. .@hotmail.co.uk wrote:
Hi, I have a not simple problem.

I want the following XML to be schema valid

<PopulationDemo graphic>
<PersonName>J oe Blogs</PersonName>
<Age>2</Age>
<Category>bab y</Category>
</PopulationDemog raphic>

And I want the following XML to be schema invalid

<PopulationDemo graphic>
<PersonName>J oe Blogs</PersonName>
<Age>2</Age>
<Category>teena ger</Category>
</PopulationDemog raphic>

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="Populatio nDemographic">
<xs:complexType >
<xs:sequence>
<xs:element name="PersonNam e" type="xs:string "/>
<xs:element name="Age" type="ages"/>
<xs:element name="Category" type="category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleTyp e name="category" >
<xs:restricti on base="xs:string ">
<xs:enumerati on value="baby"/>
<xs:enumerati on value="todler"/>
<xs:enumerati on value="teenager "/>
<xs:enumerati on value="pensione r"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleTyp e name="ages">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="1"/>
<xs:enumerati on value="2"/>
<xs:enumerati on value="3"/>
<xs:enumerati on 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:simpleTyp e name="ages">
<xs:restricti on base="xs:int">
<xs:minInclusiv e value="0"/>
<xs:maxInclusiv e 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)
=============== =============== ===============
Feb 16 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2800
by: linkspeed | last post by:
Hi, I tried following element definition in MSXML 4.0 <xsd:element name="Identifier"> <xsd:simpleType> <xsd:restriction base="xsd:token"> <xsd:pattern value=".*"/> </xsd:restriction> </xsd:simpleType> </xsd:element> It can validate XML text like: <Identifier>abbbb</Identifier>
1
3222
by: Andrey Brozhko | last post by:
Hi I need to represent custom type system (ints, bytes, chars, enums, bitmasks, arrays and some other types) in xml. It is easy to see how to represent enums in xml (using xs:enumeration), the same is true for numeric types. Even arrays have an obvious description in schema: <complexType name="IntArray"> <sequence> <element name="item" type="int" minOccurs="0" maxOccurs="unbounded"/>
1
18383
by: Derek | last post by:
I am writing a web application for job seekers, and am adding a section that allows administrators to upload jobs to the system via xml. I have used an xml schema to validate the xml they upload. The website runs on IIS5 using classic ASP, and the xml validation is done in a .NET component written in C# that is registered for COM interop on the webserver. I have a test application to test the .NET component, and that application does...
0
1334
by: Codex Twin | last post by:
hello group: The following is a fragment from a schema which defines the EWethnicCategoryStructure type. As you can see, its type is defined by the SimpleType enumeration EWethnicCategoryType. EWethnicCategoryType is defined as a union with the CommonEthnicCategoryType SimpleType enumeration. Now when I use the XSD tool to create a C# class of this schema, the resulting code does not include the SelectedEthnicCategory enumeration.
4
2293
by: bibsoconner | last post by:
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all works with SPY, but when I pick "Validate Schema" from the .NET 2003 menu, it fails with message: "Type XType is not declared." As I hinted at in my subject line, I suspect that it has to do with including another schema multiple times.
3
2765
by: ciaran.mchale | last post by:
Hi folks, I downloaded the binary version of Xerces C++ 2.7.0 for Windows and am using it to help me get up to speed with XML and XML Schema. So please excuse me if this is a "novice" question. In the samples/data directory, I ran the following command for all the files: DOMPrint -n -s -f -v=always <filename>
1
4747
by: tankbattle | last post by:
That is, what's the difference between <complexType name="Address" final="restriction"> <sequence> <element name="name" type="string"/> <element name="street" type="string"/> <element name="city" type="string"/> </sequence> </complexType> and <complexType name="Address" block="restriction">
6
20827
by: Thea | last post by:
Hi I am trying to use datatypes defined in xml file to check correctness of input parameter values To define needed datatypes following schema.xml file was created: <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pd="uri:myUri.com"> <xsd:simpleType name="pd:width">
4
4821
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I don't really understand how these namespace work in xml. (I am however aware of what problems namespaces solve) I'm not even sure if the provided xsd is 'common' practice, although it validates correctly. So I'll describe exactly what I've...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.