Question on XML schema using "restriction"

Discussion in 'XML' started by brucepickford001@hotmail.co.uk, Feb 16, 2007.

  1. Guest

    Hi, I have a not simple problem.

    I want the following XML to be schema valid

    <PopulationDemographic>
    <PersonName>Joe Blogs</PersonName>
    <Age>2</Age>
    <Category>baby</Category>
    </PopulationDemographic>

    And I want the following XML to be schema invalid

    <PopulationDemographic>
    <PersonName>Joe Blogs</PersonName>
    <Age>2</Age>
    <Category>teenager</Category>
    </PopulationDemographic>

    I can see how to create the restriction of "baby", "todler",
    "teenager" and "pensioner" for my element "Category". I can see how to
    restrict the ages to one of "1", "2", "3" ... "110" using similar
    code. But I can't work out how to say in the schema

    if "Category" = "baby" then valid ages must be "1", "2"
    if "Category" = "todler" then valid ages must be "2", "3", "4" and "5"
    if "Category" = "teenager" then valid ages must be "13", "14", "15",
    "16", "17", "18" and "19"
    if "Category" = "pensioner" then valid ages must be "65", "66" ....
    "110"


    Here is my schema now

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="PopulationDemographic">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="PersonName" type="xs:string"/>
    <xs:element name="Age" type="ages"/>
    <xs:element name="Category" type="category"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:simpleType name="category">
    <xs:restriction base="xs:string">
    <xs:enumeration value="baby"/>
    <xs:enumeration value="todler"/>
    <xs:enumeration value="teenager"/>
    <xs:enumeration value="pensioner"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="ages">
    <xs:restriction base="xs:string">
    <xs:enumeration value="1"/>
    <xs:enumeration value="2"/>
    <xs:enumeration value="3"/>
    <xs:enumeration value="4"/>
    <!-- all the others up to 110 -->
    </xs:restriction>
    </xs:simpleType>
    </xs:schema>

    How can I do this? I have a feeling there is going to be an xpath and
    keyref here somewhere but don't have the skills yet to know this for
    certain or write the code (I am using xmlspy and the book xml in a
    nutshell).

    If anyone has an online reference, a pseudo code snippet, or help me
    out with extending above for say todlers (I will do the rest) I would
    really appreciate it.

    This is my first post to newsgroup so sorry to have to ask a nasty
    question like this without contributing anything first.
    Thank you

    Bruce
    , Feb 16, 2007
    #1
    1. Advertising

  2. Guest

    On 16 Feb, 15:23, wrote:
    > Hi, I have a not simple problem.
    >
    > I want the following XML to be schema valid
    >
    > <PopulationDemographic>
    > <PersonName>Joe Blogs</PersonName>
    > <Age>2</Age>
    > <Category>baby</Category>
    > </PopulationDemographic>
    >
    > And I want the following XML to be schema invalid
    >
    > <PopulationDemographic>
    > <PersonName>Joe Blogs</PersonName>
    > <Age>2</Age>
    > <Category>teenager</Category>
    > </PopulationDemographic>
    >
    > I can see how to create the restriction of "baby", "todler",
    > "teenager" and "pensioner" for my element "Category". I can see how to
    > restrict the ages to one of "1", "2", "3" ... "110" using similar
    > code. But I can't work out how to say in the schema
    >
    > if "Category" = "baby" then valid ages must be "1", "2"
    > if "Category" = "todler" then valid ages must be "2", "3", "4" and "5"
    > if "Category" = "teenager" then valid ages must be "13", "14", "15",
    > "16", "17", "18" and "19"
    > if "Category" = "pensioner" then valid ages must be "65", "66" ....
    > "110"
    >
    > Here is my schema now
    >
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    > <xs:element name="PopulationDemographic">
    > <xs:complexType>
    > <xs:sequence>
    > <xs:element name="PersonName" type="xs:string"/>
    > <xs:element name="Age" type="ages"/>
    > <xs:element name="Category" type="category"/>
    > </xs:sequence>
    > </xs:complexType>
    > </xs:element>
    > <xs:simpleType name="category">
    > <xs:restriction base="xs:string">
    > <xs:enumeration value="baby"/>
    > <xs:enumeration value="todler"/>
    > <xs:enumeration value="teenager"/>
    > <xs:enumeration value="pensioner"/>
    > </xs:restriction>
    > </xs:simpleType>
    > <xs:simpleType name="ages">
    > <xs:restriction base="xs:string">
    > <xs:enumeration value="1"/>
    > <xs:enumeration value="2"/>
    > <xs:enumeration value="3"/>
    > <xs:enumeration value="4"/>
    > <!-- all the others up to 110 -->
    > </xs:restriction>
    > </xs:simpleType>
    > </xs:schema>
    >
    > How can I do this? I have a feeling there is going to be an xpath and
    > keyref here somewhere but don't have the skills yet to know this for
    > certain or write the code (I am using xmlspy and the book xml in a
    > nutshell).
    >
    > If anyone has an online reference, a pseudo code snippet, or help me
    > out with extending above for say todlers (I will do the rest) I would
    > really appreciate it.
    >
    > This is my first post to newsgroup so sorry to have to ask a nasty
    > question like this without contributing anything first.
    > Thank you
    >
    > Bruce


    Hi Bruce,

    Firstly I suggest you make your 'age' type similar to

    <xs:simpleType name="ages">
    <xs:restriction base="xs:int">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="110"/>
    </xs:restriction>
    </xs:simpleType>

    In terms of checking your categories are the right age range,
    currently this is not possible in XSD alone. The current approach to
    such problems seems to be to suggest using Schematron embedded in the
    schema to specify the constraints (http://www.schematron.com/). The
    current version of XSD 1.1 being developed is likely to allow such
    constraints to be specified, but there is no fixed date for when it
    will be released yet.

    It might just be easier to check it at the application level!

    HTH,

    Pete.
    =============================================
    Pete Cordell
    Tech-Know-Ware Ltd
    for XML to C++ data binding visit
    http://www.tech-know-ware.com/lmx
    (or http://www.xml2cpp.com)
    =============================================
    , Feb 16, 2007
    #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. Markus
    Replies:
    1
    Views:
    1,519
    Markus
    Nov 23, 2005
  2. Replies:
    1
    Views:
    442
    Stan Kitsis [MSFT]
    Dec 29, 2004
  3. Replies:
    1
    Views:
    382
    Stan Kitsis [MSFT]
    Dec 29, 2004
  4. Replies:
    3
    Views:
    467
    Henry S. Thompson
    Jan 7, 2005
  5. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,244
    Stanimir Stamenkov
    Apr 25, 2005
Loading...

Share This Page