Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Choice

Status
Not open for further replies.

RwDwR

Programmer
Aug 1, 2002
16
NL
I have just started using XML, but among many things I have to do the following:

Within my xml file I have the need for a name tag that can exist in two ways:
Code:
1. <Name>John Doe</Name>
2. <Name>
     <First>John</First>
     <Last>Doe</Last>
   </Name>
It must be one or the other, how can I declare this using an XML-Schema??
I think the problem lies in the first option being a simple type, and the second a complex type...

Well, any help would be great, thanks
 
This seams to give me the effect I want, but I am not happy about it:
Code:
<xs:element name=&quot;name&quot;>
 <xs:complexType mixed=&quot;true&quot;>
   <xs:choice>
   <xs:any minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;></xs:any>
   <xs:sequence>
    <xs:element ref=&quot;first&quot;/>
    <xs:element ref=&quot;last&quot;/>
   </xs:sequence>
  </xs:choice>
 </xs:complexType>
</xs:element>

The problem is that the any tag allows, well, litteraly any one thing, wheter that be tags or text, and I only want to allow text, any ideas?
 
I am not sure you can have that... A cleaner approach would be to define two types for each form you have and have a &quot;personName&quot; complex type that would make use of choices for each type. However, in this case, you will notice below in the <PersonName> type that you cannot have the same <name> element for both choices, as it is considered inconsistent and illegal by many parsers.

<xs:complexType name=&quot;SName&quot;>
<xs:simpleContent>
<xs:extension base=&quot;xs:string&quot;/>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name=&quot;CName&quot;>
<xs:sequence>
<xs:element name=&quot;First&quot;/>
<xs:element name=&quot;Last&quot;/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=&quot;PersonName&quot;>
<xs:choice>
<xs:element name=&quot;SepName&quot; type=&quot;CName&quot;/>
<xs:element name=&quot;SimpleName&quot; type=&quot;SName&quot;></xs:element>
</xs:choice>
</xs:complexType>

Also, see the post thread426-314884 it will help... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
You know, I really hate these languages that say they are good on every aspect, and stuff, I can't even declare such a simple thing like this?? The thread you put on the bottom does work, but it allows text to be present togheter with the tags, I'll go for my any tag for now, and hope nobody finds out ;)
 
Remember: XML is extensible. If you dont like it, extend it :0)



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top