XML Schema problem...

Discussion in 'XML' started by Andrew, Dec 18, 2003.

  1. Andrew

    Andrew Guest

    Antony (4:26 PM) :
    Hello all !

    I have a problem with XML schema.

    My XML document which I need to be validated
    can be one of the following two types:

    1st type is

    <?xml version="1.0"?>
    <response>
    <msg_id>100</msg_id>
    <command>Command1</command>
    <time>100</time>
    <b>text</b>
    </response>


    2nd type is

    <?xml version="1.0"?>
    <response>
    <msg_id>100</msg__id>
    <command>Command2</command>
    <sometag>content</sometag>
    <time>789012</time>
    <a>text</a>
    </response>


    Note that content of the "command" element
    is used to determine the rest of elements. Thus
    "Command1" means that there should be "time" and "b" elements,
    and "Command2" means that there should be "sometag", "time", and "a"
    elements.

    When trying to load the schema both xerces and msxml parsers
    return me error message: "Unique Particle Attribution rule".

    How should correct schema look ?
    Can anyone help ?

    Thanks in advance.
    Andrew, Dec 18, 2003
    #1
    1. Advertising

  2. Andrew

    Andrew Guest

    This is my schema:

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

    <xs:element name="response" type="respType"/>

    <xs:complexType name="respType">
    <xs:choice>
    <xs:group ref="g1"/>
    <xs:group ref="g2"/>
    </xs:choice>
    </xs:complexType>

    <xs:group name="g1">
    <xs:sequence>
    <xs:element name="msg_id" type="xs:int"/>
    <xs:element name="command" type="xs:string"/>
    <xs:element name="time" type="xs:int"/>
    <xs:element name="b" type="xs:string"/>
    </xs:sequence>
    </xs:group>

    <xs:group name="g2">
    <xs:sequence>
    <xs:element name="msg_id" type="xs:int"/>
    <xs:element name="command" type="xs:string"/>
    <xs:element name="sometag" type="xs:string"/>
    <xs:element name="time" type="xs:int"/>
    <xs:element name="a" type="xs:string"/>
    </xs:sequence>
    </xs:group>

    </xs:schema>
    Andrew, Dec 18, 2003
    #2
    1. Advertising

  3. Andrew

    Sean Bright Guest

    Andrew wrote:

    > Note that content of the "command" element
    > is used to determine the rest of elements. Thus
    > "Command1" means that there should be "time" and "b" elements,
    > and "Command2" means that there should be "sometag", "time", and "a"
    > elements.


    I'm not sure this type of conditional processing is possible with XML
    Schemas. See the the following thread in this newsgroup for something
    slightly similar:

    Message ID: <>
    From: Ralf Wahner
    Subject: Schema express that "@a present if and only if @b present",
    where @a, @b are attributes

    The relevant portion of that thread is:

    >> As I'm new to XML Schema I dare to ask a possibly recurring
    >> question: Given an element <elem> with two attributes @a and
    >> @b. The attributes are bound by the condition, that either both
    >> or none must be present, i.e.

    >
    > That is not possible with W3C's xml schema.


    Apparently something like this is possible with RELAX NG, but I wouldn't
    know about that.

    Hope this helps,
    Sean
    Sean Bright, Dec 18, 2003
    #3
  4. Andrew

    strajan Guest

    Please try whether the following work. Just re-arranged a little bit.
    =============
    <?xml version="1.0" ?>

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="response" type="respType" />

    <xs:complexType name="respType">

    <xs:sequence>

    <xs:element name="msg_id" type="xs:int" />

    <xs:element name="command" type="xs:string" />

    <xs:choice>

    <xs:group ref="g1" />

    <xs:group ref="g2" />

    </xs:choice>

    </xs:sequence>

    </xs:complexType>

    <xs:group name="g1">

    <xs:sequence>

    <xs:element name="time" type="xs:int" />

    <xs:element name="b" type="xs:string" />

    </xs:sequence>

    </xs:group>

    <xs:group name="g2">

    <xs:sequence>

    <xs:element name="sometag" type="xs:string" />

    <xs:element name="time" type="xs:int" />

    <xs:element name="a" type="xs:string" />

    </xs:sequence>

    </xs:group>

    </xs:schema>

    ================

    "Andrew" <> wrote in message
    news:brprcb$2uc1$...
    > This is my schema:
    >
    > <?xml version="1.0"?>
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    >
    > <xs:element name="response" type="respType"/>
    >
    > <xs:complexType name="respType">
    > <xs:choice>
    > <xs:group ref="g1"/>
    > <xs:group ref="g2"/>
    > </xs:choice>
    > </xs:complexType>
    >
    > <xs:group name="g1">
    > <xs:sequence>
    > <xs:element name="msg_id" type="xs:int"/>
    > <xs:element name="command" type="xs:string"/>
    > <xs:element name="time" type="xs:int"/>
    > <xs:element name="b" type="xs:string"/>
    > </xs:sequence>
    > </xs:group>
    >
    > <xs:group name="g2">
    > <xs:sequence>
    > <xs:element name="msg_id" type="xs:int"/>
    > <xs:element name="command" type="xs:string"/>
    > <xs:element name="sometag" type="xs:string"/>
    > <xs:element name="time" type="xs:int"/>
    > <xs:element name="a" type="xs:string"/>
    > </xs:sequence>
    > </xs:group>
    >
    > </xs:schema>
    >
    >
    >
    >
    strajan, Dec 18, 2003
    #4
  5. Andrew wrote:
    > <xs:element name="command" type="xs:string"/>
    > <xs:element name="command" type="xs:string"/>


    Sorry about the late answer, but why not create your own types with a
    pattern restriction?

    Blatantly stolen from http://www.w3.org/TR/xmlschema-2/#rf-pattern:

    <simpleType name=command1>
    <restriction base='string'>
    <pattern value='Command1'/>
    </restriction>
    </simpleType>

    <simpleType name='command2'>
    <restriction base='string'>
    <pattern value='Command2'/>
    </restriction>
    </simpleType>

    --
    Jakob Møbjerg Nielsen | "Nine-tenths of the universe is
    | knowledge of the position and direction
    http://www.jakobnielsen.dk/ | of everything in the other tenth."
    | -- Terry Pratchett, Thief of Time
    Jakob Møbjerg Nielsen, Dec 21, 2003
    #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. Markus
    Replies:
    1
    Views:
    1,497
    Markus
    Nov 23, 2005
  2. Fred Smith
    Replies:
    1
    Views:
    924
    Martin Honnen
    Feb 5, 2004
  3. Hari Om
    Replies:
    1
    Views:
    449
    Martin SChukrazy
    Mar 2, 2004
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,170
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    3
    Views:
    2,788
Loading...

Share This Page