XML schema regular expressions question and recommended XML Schema book

Discussion in 'XML' started by Fred Smith, Feb 4, 2004.

  1. Fred Smith

    Fred Smith Guest

    Any suggestions or tips to the questions below I have been wrestling with
    would be most welcome:

    I have an example XSD file I have been experimenting with. Suppose a
    user can select from 1 to 4 checkboxes on a web form:

    Select your favorite sport(s):

    (checkbox) baseball
    (checkbox) football
    (checkbox) soccer
    (checkbox) golf

    Futhermore, suppose a user selects all the above sports:
    The xml generated from the program that reads the form variables would be:

    <sport>baseball\n\football\n\soccer\n\golf</sport>

    Questions:

    1. what is the most effective schema statement to handle the
    possiblity of the user selecting either baseball or baseball and football
    or baseball, football, soccer etc..? In other words, any permutation of
    checkboxes.

    I would think you would use the logical or operator (|).
    I was thinking something like below, but I am getting errors such as
    "Datatype error: In element 'sport': Value ........ does not match regular
    expression facet ................

    <xsd:simpleType name="sport">
    <xsd:restriction base="xsd:string">
    <xsd:pattern value="baseball\\n|\\nfootball\\n|\\nsoccer\\n|golf"/>

    2. Does anyone recommend any books that explain XML Schema Regular Expressions
    with some good examples? I have read Definitive XML Schema, by Walmsley.

    Thanks for any tips on the above,

    Fred
     
    Fred Smith, Feb 4, 2004
    #1
    1. Advertising

  2. Re: XML schema regular expressions question and recommended XMLSchema book

    Fred Smith wrote:

    > Any suggestions or tips to the questions below I have been wrestling with
    > would be most welcome:
    >
    > I have an example XSD file I have been experimenting with. Suppose a
    > user can select from 1 to 4 checkboxes on a web form:
    >
    > Select your favorite sport(s):
    >
    > (checkbox) baseball
    > (checkbox) football
    > (checkbox) soccer
    > (checkbox) golf
    >
    > Futhermore, suppose a user selects all the above sports:
    > The xml generated from the program that reads the form variables would be:
    >
    > <sport>baseball\n\football\n\soccer\n\golf</sport>
    >
    > Questions:
    >
    > 1. what is the most effective schema statement to handle the
    > possiblity of the user selecting either baseball or baseball and football
    > or baseball, football, soccer etc..? In other words, any permutation of
    > checkboxes.
    >
    > I would think you would use the logical or operator (|).
    > I was thinking something like below, but I am getting errors such as
    > "Datatype error: In element 'sport': Value ........ does not match regular
    > expression facet ................
    >
    > <xsd:simpleType name="sport">
    > <xsd:restriction base="xsd:string">
    > <xsd:pattern value="baseball\\n|\\nfootball\\n|\\nsoccer\\n|golf"/>


    If there are certain fixed values possible a list of an enumerated type
    could help:

    <?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="sport" maxOccurs="unbounded" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:simpleType name="sport">
    <xs:restriction base="xs:string">
    <xs:enumeration value="baseball" />
    <xs:enumeration value="football" />
    <xs:enumeration value="soccer" />
    <xs:enumeration value="golf" />
    </xs:restriction>
    </xs:simpleType>

    <xs:element name="sport">
    <xs:simpleType>
    <xs:list itemType="sport" />
    </xs:simpleType>
    </xs:element>

    </xs:schema>

    That allows values like

    <?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test20040205Xsd.xml">
    <sport>baseball football soccer golf</sport>
    <sport>baseball
    football
    soccer
    golf
    </sport>
    <sport>soccer football</sport>
    </root>

    meaning the order is not restricted to what you have.


    If you want to use a regular expression to restrict the type I think the
    following helps:

    <?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="sport" maxOccurs="unbounded" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name="sport">
    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:pattern
    value="(baseball\s*)?(football\s*)?(soccer\s*)?(golf)?" />
    </xs:restriction>
    </xs:simpleType>
    </xs:element>

    </xs:schema>

    which restricts the content of a <sport> element to being zero or one
    occurrence of baseball plus some whitespace and zero or one occurence of
    football plus some whitespace and zero or one occurence of soccer plus
    some whitespace plus zero or one occurence of golf.
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 5, 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. Jay Douglas
    Replies:
    0
    Views:
    643
    Jay Douglas
    Aug 15, 2003
  2. Markus
    Replies:
    1
    Views:
    1,671
    Markus
    Nov 23, 2005
  3. Jeff
    Replies:
    1
    Views:
    1,297
    Joris Gillis
    Feb 25, 2005
  4. Kaye Ng
    Replies:
    1
    Views:
    150
    Bob Proulx
    Aug 30, 2010
  5. Noman Shapiro
    Replies:
    0
    Views:
    260
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page