XSD restrictions - both string and numerical

Discussion in 'XML' started by voorth, Feb 12, 2008.

  1. voorth

    voorth Guest

    I was wondering if it is possible to define an restricted attribute
    such that the following forms are both valid:

    <myElement value="hi"/> <!-- value should be "hi" or "lo" -->
    <myElement value="100"/><!-- value should be between 50-150 -->
    voorth, Feb 12, 2008
    #1
    1. Advertising

  2. voorth wrote:
    > I was wondering if it is possible to define an restricted attribute
    > such that the following forms are both valid:
    >
    > <myElement value="hi"/> <!-- value should be "hi" or "lo" -->
    > <myElement value="100"/><!-- value should be between 50-150 -->


    You can define a union type of two simple types:

    <xs:attribute name="value">
    <xs:simpleType>
    <xs:union>
    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:enumeration value="hi"/>
    <xs:enumeration value="lo"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType>
    <xs:restriction base="xs:int">
    <xs:minInclusive value="50"/>
    <xs:maxInclusive value="100"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:union>
    </xs:simpleType>
    </xs:attribute>

    See section "2.5.1.3 Union datatypes" in
    <URL:http://www.w3.org/TR/xmlschema-2/#datatype-dichotomies>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Feb 12, 2008
    #2
    1. Advertising

  3. voorth

    Guest

    On 12 Feb, 11:33, voorth <> wrote:
    > I was wondering if it is possible to define an restricted attribute
    > such that the following forms are both valid:
    >
    > <myElement value="hi"/> <!-- value should be "hi" or "lo" -->
    > <myElement value="100"/><!-- value should be between 50-150 -->


    You can use XML schema xs:union construct to do this. Something along
    the lines of:

    <xs:attribute name="value">
    <xs:simpleType>
    <xs:union>
    <xs:simpleType>
    <xs:restriction base="xs:int">
    <xs:minInclusive value="50"/>
    <xs:maxInclusive value="150"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:enumeration value="hi"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:union>
    </xs:simpleType>
    </xs:attribute>

    Although you probably don't want the string part as restricted as
    this!

    HTH,

    Pete Cordell
    Codalogic
    Visit http://www.codalogic.com/lmx/ for XML C++ data binding
    , Feb 12, 2008
    #3
  4. voorth

    Guest

    On 12 Feb, 12:41, wrote:
    > On 12 Feb, 11:33, voorth <> wrote:
    >
    > > I was wondering if it is possible to define an restricted attribute
    > > such that the following forms are both valid:

    >
    > ...
    >
    > Although you probably don't want the string part as restricted as
    > this!


    Ooops, missed the 'lo' part. See Martin's answer for that. He must
    type faster than me!

    HTH,

    Pete Cordell
    Codalogic
    Visit http://www.codalogic.com/lmx/ for XML C++ data binding
    , Feb 12, 2008
    #4
  5. voorth

    voorth Guest

    Thanks, guys, that was just what I was looking for.
    voorth, Feb 12, 2008
    #5
    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. TIANA

    Restrictions in XSD

    TIANA, Dec 7, 2004, in forum: XML
    Replies:
    0
    Views:
    407
    TIANA
    Dec 7, 2004
  2. Dmitry Kulinich
    Replies:
    5
    Views:
    535
  3. ABCL
    Replies:
    0
    Views:
    513
  4. Arinté

    XSD restrictions/extensions

    Arinté, Dec 18, 2008, in forum: XML
    Replies:
    0
    Views:
    740
    Arinté
    Dec 18, 2008
  5. rajesh
    Replies:
    0
    Views:
    166
    rajesh
    Nov 23, 2005
Loading...

Share This Page