Developing a contrained based XSD?

C

conradwt

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
 
C

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
 
S

Stanimir Stamenkov

/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.
 
C

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
 
S

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.
 
S

Stanimir Stamenkov

/Stanimir Stamenkov/:
/Con/:


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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top