473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

xsd.exe and choice

Hi there!

I have the following XSD (Well, this is not the real one, but the
concept is the same....)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="DataItemType">
<xs:choice>
<xs:element name="Item1" type="Item1Type"/>
<xs:sequence>
<xs:element ref="Item2"/>
<xs:element ref="Item3"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexType name="Item1Type">
<xs:sequence>
<xs:element ref="Item1a"/>
<xs:element ref="Item1b"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Item1a" type="xs:string"/>
<xs:element name="Item1b" type="xs:string"/>
<xs:element name="Item2" type="xs:string"/>
<xs:element name="Item3" type="xs:string"/>
<xs:element name="MyData">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="DataItem"
type="DataItemType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

So far, so good. You see the xs:choice item, that consists of either the
complex type ItemType, or a sequence of Item2 and Item3 (both simple
strings).

So.. these are both valid xml files:

<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\mydata.xsd">
<DataItem>
<Item1>
<Item1a>Value1</Item1a>
<Item1b>Value2</Item1b>
</Item1>
</DataItem>
</MyData>
and...

<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\mydata.xsd">
<DataItem>
<Item2>Value1</Item2>
<Item3>Value2</Item3>
</DataItem>
</MyData>

Right. The XSD.Exe tool makes this of my XSD:

//----------------------------------------------------------------------
--------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be
lost if
// the code is regenerated.
// </autogenerated>
//----------------------------------------------------------------------
--------

//
// This source code was auto-generated by xsd, Version=1.1.4322.573.
//
using System.Xml.Serialization;
/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)]
public class MyData {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Data Item")]
public DataItemType[] DataItem;
}

/// <remarks/>
public class DataItemType {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", typeof
(Item1Type))]
[System.Xml.Serialization.XmlChoiceIdentifierAttrib ute
("ItemElementName")]
public object Item;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName;
}

/// <remarks/>
public class Item1Type {

/// <remarks/>
public string Item1a;

/// <remarks/>
public string Item1b;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(IncludeI nSchema=false)]
public enum ItemChoiceType {

/// <remarks/>
Item2,

/// <remarks/>
Item3,

/// <remarks/>
Item1,
}

Now, this doesn't seem right. I can now have in my XML file an Item1,
and Item2 OR an Item3, instead of an Item1 OR (an Item2 AND an Item3).

Can anybody help me out here? What is it that I am doing wrong?

Thanks in advance
Dennis Vroegop
Nov 11 '05 #1
4 8978
Hi Dennis,

I have sent your question on to one of our XML experts. I will reply with
his answer ASAP.

Brett Keown
Microsoft Support
br*****@online.microsoft.com

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 11 '05 #2
Dennis,

Looks like the XSD tool doesn't account for a sequence definition inside a
choice model group ...

This may not be a bug as much as it is a design limitation of the tool and
the XmlSerializer. There is no way to express that Item2 and Item3 belong
together, since they appear at the same level of the document.

What you can do is to make the two fields that identify the choice array
types:

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", typeof
(Item1Type))]
[System.Xml.Serialization.XmlChoiceIdentifierAttrib ute
("ItemElementName")]
public object[] Item;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType[] ItemElementName;

Now you can at least assign Item2 and Item3 values.

Unfortunately. I can't think of a better way to work around this. You still
need to make sure you have schema valid document by validating the
serialized document to against your schema, or make sure in code that you
never assign invalid combinations Item1, Item2 and Item2 to the Item[]

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Dennis Vroegop" <dv******@detrio.nl> wrote in message
news:MP************************@news.xs4all.nl...
Hi there!

I have the following XSD (Well, this is not the real one, but the
concept is the same....)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="DataItemType">
<xs:choice>
<xs:element name="Item1" type="Item1Type"/>
<xs:sequence>
<xs:element ref="Item2"/>
<xs:element ref="Item3"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexType name="Item1Type">
<xs:sequence>
<xs:element ref="Item1a"/>
<xs:element ref="Item1b"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Item1a" type="xs:string"/>
<xs:element name="Item1b" type="xs:string"/>
<xs:element name="Item2" type="xs:string"/>
<xs:element name="Item3" type="xs:string"/>
<xs:element name="MyData">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="DataItem"
type="DataItemType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

So far, so good. You see the xs:choice item, that consists of either the
complex type ItemType, or a sequence of Item2 and Item3 (both simple
strings).

So.. these are both valid xml files:

<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\mydata.xsd">
<DataItem>
<Item1>
<Item1a>Value1</Item1a>
<Item1b>Value2</Item1b>
</Item1>
</DataItem>
</MyData>
and...

<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\mydata.xsd">
<DataItem>
<Item2>Value1</Item2>
<Item3>Value2</Item3>
</DataItem>
</MyData>

Right. The XSD.Exe tool makes this of my XSD:

//----------------------------------------------------------------------
--------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be
lost if
// the code is regenerated.
// </autogenerated>
//----------------------------------------------------------------------
--------

//
// This source code was auto-generated by xsd, Version=1.1.4322.573.
//
using System.Xml.Serialization;
/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)]
public class MyData {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Data Item")]
public DataItemType[] DataItem;
}

/// <remarks/>
public class DataItemType {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", typeof
(Item1Type))]
[System.Xml.Serialization.XmlChoiceIdentifierAttrib ute
("ItemElementName")]
public object Item;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName;
}

/// <remarks/>
public class Item1Type {

/// <remarks/>
public string Item1a;

/// <remarks/>
public string Item1b;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(IncludeI nSchema=false)]
public enum ItemChoiceType {

/// <remarks/>
Item2,

/// <remarks/>
Item3,

/// <remarks/>
Item1,
}

Now, this doesn't seem right. I can now have in my XML file an Item1,
and Item2 OR an Item3, instead of an Item1 OR (an Item2 AND an Item3).

Can anybody help me out here? What is it that I am doing wrong?

Thanks in advance
Dennis Vroegop

Nov 11 '05 #3
Hi Dennis,

There are known issues and limiations on the Xsd.exe tool. While there are
many ways to do things in Xsd, not all of those have been implmeneted in
the tool, and some only partially.

For the problem that you are running into, I would say there is nothing
wrong with the way you have written it.

The problem is this

For the following element, how would you code it for a class

<xs:choice>
<xs:element name="Item1" type="Item1Type"/>
<xs:sequence>
<xs:element ref="Item2"/>
<xs:element ref="Item3"/>
</xs:sequence>
</xs:choice>
In this case, we have to create it as such.

public object Item.
Then how do you tell it is can be called (item1, or item2 and item3)?

Unfortunately, there isn't any easy way, and to make a class out of it,
would be incorrect.

So, it does its best to "fudge" it. and throws on the following

[System.Xml.Serialization.XmlElementAttribute("Item 2", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 3", typeof
(string))]
[System.Xml.Serialization.XmlElementAttribute("Item 1", typeof
(Item1Type))]

It is difficult to sometimes put into code what is possible in Xsd.

Bruce Taimana

Microsoft Developer Support XML WebData Group

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.

© 2001 Microsoft Corporation. All rights reserved.

Nov 11 '05 #4
In article <uL**************@TK2MSFTNGP11.phx.gbl>,
ch********************@austin.rr.com says...
Dennis,

Looks like the XSD tool doesn't account for a sequence definition inside a
choice model group ...

Thank you all for your help and your time. I can go on with the project
with the solutions you all proposed. I agree that the XSD as I presented
it isn't the right way to do it (Item2 and Item3 should have been in a
complextype, imho) but there is nothing I can do about that. I have to
deal with whatever they give me :-(

Anyway, I can continue now. Thanks!

Dennis Vroegop
Detrio Consultancy b.v.
Nov 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

198
by: Sridhar R | last post by:
>From technical point of view, I could not understand the the reasoning behind using Java in major companies. Sure that Python, is used in some, but still Java is considered as a sure-job...
3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
3
by: kosaraju.puneeth | last post by:
I want an element that must have either attribute A or B. <Thing1 A="aaa"/> <Thing1 B="bbb"/> I want to enforce this in my schema . If A and B were elements, I could use choice.
1
by: Oleg Ogurok | last post by:
Hi all, I have a complex type defined as follows: <xs:complexType name="SchedulingMethodType"> <xs:choice maxOccurs="1"> <xs:element name="Interval" type="xs:duration" /> <xs:element...
4
by: Sergey Poberezovskiy | last post by:
Hi, As part of my schema I need to ensure that at least one of two fields have values. I defined my schema as follows: .... <xs:choice> <xs:element ref="el1"/> <xs:element ref="el2"/>...
7
by: rbarschaw | last post by:
I have the following schema designed: <xs:complexType name="AzzFeature-BoxType" mixed="true"> <xs:choice minOccurs="1" maxOccurs="unbounded"> <xs:element ref="Sub-Head" minOccurs="1"...
1
by: SL33PY | last post by:
Hi, I'm currently busy writing an xsd at one point in time i whish that my xml must look like: <object> <loc> <disk> <drive>c</drive> <path>temp\path</path> <files>*.*</files>...
2
by: hooomee | last post by:
Given: <xs:choice maxOccurs=5> <xs:element name="Foo" type="bar" /> <xs:element name="Foo1" type="bar" /> <xs:element name="Foo2" type="bar" /> </xs:choice> Is the choice made once and then...
0
by: Peter Larsen | last post by:
Is this really a valid schema design? <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="root">...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.