473,503 Members | 12,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Developing a contrained based XSD?

Hi, I was wondering, is it possible to restrict the value of an element
based on the type element? For example,

<mytypes>
<mytype>
<name>value1</name>
<type>double</type>
<value>123.89</value>
</mytype>

<mytype>
<name>value2</name>
<type>integer</type>
<value>123</value>
</mytype>

<mytype>
<name>value3</name>
<type>short</type>
<value>123</value>
</mytype>
</mytypes>

Now, the specification would for the XSD would be as follows:

a) 1 or more 'mytype'
b) 'type' element should constrain 'value'

If anyone has any ideas as to how to develop this in XSD, please post
to the group.

Thanks in advance,

-Conrad

Jul 20 '05 #1
6 2202


co******@runbox.com wrote:
Hi, I was wondering, is it possible to restrict the value of an element
based on the type element? For example,

<mytypes> <mytypes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<mytype>
<name>value1</name>
<type>double</type>
<value>123.89</value>


You can use stuff like
<value xsi:type="xs:double">123.89</value>
if you want to refer to the types XML schema knows.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
Con
Hi, thanks to responding to my post. Anyway, if type tag is of the
following:

short
int
long int
float
double

How does enforce the type of value in the value tag?

Thanks in advance,

-Conrad

Jul 20 '05 #3
/Con/:
Hi, thanks to responding to my post. Anyway, if type tag is of the
following:

short
int
long int
float
double

How does enforce the type of value in the value tag?


Define a base type for your "mytype" element, like:

<xs:complexType name="MyType">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="type" type="ValueTypes" />
<xs:element name="value" type"xs:anySimpleType" />
</xs:sequence>
</xs:complexType>

<xs:element name="mytype" type="MyType" />

Define the "ValueTypes":

<xs:simpleType name="ValueTypes">
<xs:restriction base="xs:string">
<xs:enumeration value="short" />
<xs:enumeration value="int" />
<xs:enumeration value="long int" />
<xs:enumeration value="float" />
<xs:enumeration value="double" />
</xs:restriction>
</xs:simpleType>

Define the derived types:

<xs:complexType name="MyShortType">
<xs:complexContent>
<xs:restriction base="MyType">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="type" type="ValueTypes" fixed="short" />
<xs:element name="value" type"xs:short" />
</xs:sequence>
</xs:restriction>
<xs:complexContent>
</xs:complexType>

<!-- and so on -->

Then use the xsi:type attribute in the instance document:

<mytype>
<name>value2</name>
<type>int</type>
<value>ivalid still valid</value>
</mytype>

<mytype xsi:type="MyShortType">
<name>value3</name>
<type>short</type>
<value>123</value>
</mytype>

I haven't tried it but I think it should work. :-) Similar example
<http://www.w3.org/TR/xmlschema-0/#abstract> is given in the "XML
Schema Part 0: Primer", although it forces the usage of the xsi:type
attribute, declaring the base type abstract.

--
Stanimir
Jul 20 '05 #4
Con
Hey Stanimir, is it possible to not use the xsi:type="MyShortType"? I
would like to check validation based on the value of type tag and the
value tag.

Thanks in advance,

-Conrad

Jul 20 '05 #5
/Con/:
Hey Stanimir, is it possible to not use the xsi:type="MyShortType"? I
would like to check validation based on the value of type tag and the
value tag.


AFAIK, using XML Schema - it is not possible.

--
Stanimir
Jul 20 '05 #6
/Stanimir Stamenkov/:
/Con/:
Hey Stanimir, is it possible to not use the xsi:type="MyShortType"? I
would like to check validation based on the value of type tag and the
value tag.


AFAIK, using XML Schema - it is not possible.


You may use substitution groups, however... where you get
differently named elements for the different types:

...

<xs:element name="mytype" type="MyType" abstract="true" />

...

<xs:element name="myshorttype" substitutionGroup="mytype">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="MyType">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="type" minOccurs="0"
type="ValueTypes" fixed="short" />
<xs:element name="value" type"xs:short" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>

<xs:element name="myintegertype" substitutionGroup="mytype">
...
</xs:element>

...

Now wherever there's a "mytype" element particle in the schema,
there should be one of the elements from the substitution group
headed by "mytype" in the instance document. The "mytype" itself
can't appear in the instance when declared abstract in the schema.
You would notice the "type" child becomes obsolete, in the above
example.

<xs:schema ...
<xs:element name="mytypes">
<xs:complexType>
<xs:sequence>
<xs:element ref="mytype" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
...
</xs:schema>
<mytypes>

<myshorttype>
<name>value1</name>
<value>123</value>
</myshorttype>

<myintegertype>
<name>value2</name>
<value>123456789</value>
<myintegertype>

</mytypes>
Still, I suppose it is not exactly what you want. :-)
* http://www.w3.org/TR/xmlschema-0/#SubsGroups

--
Stanimir
Jul 20 '05 #7

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

Similar topics

9
1587
by: john | last post by:
Hi I am developing a timesheet system. It will have 3 levels of access 1)Administrator 2)Approver -- Will approve or reject the timesheet filled by employees 3)Employees -- Fill in the timesheet...
5
1539
by: Bill | last post by:
Good Day; I would appreciate assistance developing a query that I haven't been able to develop without using a second table. I wish to count the number of records that are still open on the...
4
2423
by: Jason Huang | last post by:
Hi, I am thinking in comparing developing speed between C# Windows Form and C# Web Form applications. Assuming the scenario is that we have 2 C# coding engineers, engineer A has 2 years...
7
1818
by: Chris Marsh | last post by:
All I've been asked run a VM on my development machine, with Windows Server 2003 installed. I've also been asked to then develop against this environment from the host machine, using Visual...
0
7212
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,...
0
7098
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...
0
7296
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,...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
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...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.