Developing a contrained based XSD?

Discussion in 'XML' started by conradwt@runbox.com, Feb 12, 2005.

  1. Guest

    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
    , Feb 12, 2005
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Feb 12, 2005
    #2
    1. Advertising

  3. Con Guest

    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
    Con, Feb 14, 2005
    #3
  4. /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
    Stanimir Stamenkov, Feb 14, 2005
    #4
  5. Con Guest

    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
    Con, Feb 15, 2005
    #5
  6. /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
    Stanimir Stamenkov, Feb 15, 2005
    #6
  7. /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
    Stanimir Stamenkov, Feb 15, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,094
    Markus
    Nov 22, 2005
  2. Rick Razzano

    XSD document for XSD defintion

    Rick Razzano, Sep 26, 2003, in forum: XML
    Replies:
    1
    Views:
    481
    C. M. Sperberg-McQueen
    Sep 26, 2003
  3. Replies:
    1
    Views:
    854
    Martin Honnen
    Jan 14, 2004
  4. Peter Aberline

    xsd:any as a child of xsd:all

    Peter Aberline, Apr 5, 2004, in forum: XML
    Replies:
    0
    Views:
    783
    Peter Aberline
    Apr 5, 2004
  5. Alex Martelli
    Replies:
    0
    Views:
    395
    Alex Martelli
    Aug 18, 2003
Loading...

Share This Page