XML Schema problem...

A

Andrew

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

Andrew

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>
 
S

Sean Bright

Andrew said:
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: <[email protected]>
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:
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
 
S

strajan

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>

================
 
J

Jakob Møbjerg Nielsen

Andrew said:
<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>
 

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

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top