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

  2. 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, Feb 12, 2008
    #2
    1. Advertisements

  3. voorth

    usenet Guest

    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
     
    usenet, Feb 12, 2008
    #3
  4. voorth

    usenet Guest

    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
     
    usenet, Feb 12, 2008
    #4
  5. voorth

    voorth Guest

    Thanks, guys, that was just what I was looking for.
     
    voorth, Feb 12, 2008
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.