restricting data types

Discussion in 'XML' started by Ranjit, Apr 10, 2004.

  1. Ranjit

    Ranjit Guest

    Hi,

    This may be quite a dumb question but I have not been able to address
    this convincibly. How can I specify restrictions on basic data types in
    my xml?

    I did some search and came across Schema Component Constraints such as
    minInclusive, maxExclusive etc.

    But I was wondering if I could refer to them in my xml.
    e.g.
    <RAM>
    <minInclusive>512</minInclusive>
    </RAM>
    -OR-
    <cost maxExclusive = "1000"/>

    or something similar.

    Can this be done?

    TIA
     
    Ranjit, Apr 10, 2004
    #1
    1. Advertising

  2. Ranjit wrote:

    > This may be quite a dumb question but I have not been able to address
    > this convincibly. How can I specify restrictions on basic data types in
    > my xml?
    >
    > I did some search and came across Schema Component Constraints such as
    > minInclusive, maxExclusive etc.
    >
    > But I was wondering if I could refer to them in my xml.
    > e.g.
    > <RAM>
    > <minInclusive>512</minInclusive>
    > </RAM>
    > -OR-
    > <cost maxExclusive = "1000"/>
    >
    > or something similar.
    >
    > Can this be done?


    You can write an XML schema that defines the structure and restrictions
    for your XML instance files, check out
    http://www.w3.org/TR/xmlschema-0/

    For instance you could define a schema as follows

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="root">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="ram" maxOccurs="unbounded" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name="ram">
    <xs:simpleType>
    <xs:restriction base="xs:positiveInteger">
    <xs:minInclusive value="512" />
    <xs:maxInclusive value="1024" />
    </xs:restriction>
    </xs:simpleType>
    </xs:element>

    </xs:schema>

    and then a validating XML parser would flag the last <ram> element in
    the following XML instance file as having a wrong value:

    <?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test20040411Xsd.xml">
    <ram>512</ram>
    <ram>768</ram>
    <ram>1024</ram>
    <ram>256</ram>
    </root>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 11, 2004
    #2
    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. chuckdfoster

    Restricting Upload File Types

    chuckdfoster, May 12, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    7,140
    Peter Blum
    May 13, 2005
  2. ramu
    Replies:
    2
    Views:
    346
    rlblaster
    Feb 20, 2006
  3. Damian
    Replies:
    2
    Views:
    614
    Damian
    Apr 7, 2008
  4. Gary Roach
    Replies:
    0
    Views:
    125
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    138
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page