tags:

views:

129

answers:

2

i want to specify that either fieldname or freetext is always present in xml files that apply to xsd. is there a way to define it?

<xs:complexType name="tSome">
<xs:sequence>
  <!-- either of 2 below have 2 be present. -->
  <xs:element name="fieldname" type="xs:string" />
  <xs:element name="freetext" type="xs:string" />
  <xs:element name="dbtablename" type="xs:string" />
</xs:sequence>
</xs:complexType>

thanks & best regards: Matti

A: 

There is a Choice Indicator in XML Schema, which allows to take 1 of the elements, but not two or more. If you want any 2 of 3, I suggest doing something like that:

<xs:choice>
  <xs:element name="fieldname" type="xs:string" minOccurs="0" maxOccures="1" />
  <xs:element name="freetext" type="xs:string" minOccurs="0" maxOccures="1" />
  <xs:element name="dbtablename" type="xs:string" minOccurs="0" maxOccures="1" />
</xs:choice>
<xs:choice>
  <xs:element name="fieldname" type="xs:string" minOccurs="0" maxOccures="1" />
  <xs:element name="freetext" type="xs:string" minOccurs="0" maxOccures="1" />
  <xs:element name="dbtablename" type="xs:string" minOccurs="0" maxOccures="1" />
</xs:choice>

(Maybe maxOccures will prevent of choosing one and the same element twice.) If that does not work, nothing will I think.

Edited: I didn't understand right the question the first time. If you want to dbtablename always present with any of 2 fieldname or freetext, then this is the answer.

<xs:complexType name="tSome">
<xs:sequence>
  <xs:choice>
    <xs:element name="fieldname" type="xs:string" />
    <xs:element name="freetext" type="xs:string" />
  </xs:choice>
  <xs:element name="dbtablename" type="xs:string" />
</xs:sequence>
</xs:complexType>
Draco Ater
thanks a lot! have to try l8r how the minoccurs and maxoccurs work with choice. i accept the answer later if it works although your example was not what i needed. cheers!
matti
sorry: weekend happened: either fieldname or freetext and not both. dbtablename always. see my edited original question (why minOccurs tags were there). My question now is: is it now really 'either fieldname or freetext and not both' since it has minOccurs tags. That is again my mistake in orig. question but ain't you think they should be removed?
matti
@matti They can be removed, as minOccures="0" does not do anything.
Draco Ater
@matti Instead you can add maxOccures="1", if you don't want elements to be specified twice or more times.
Draco Ater
matti
@draco: 'Instead you can add maxOccures="1", if you don't want elements to be specified twice or more times' what do you mean? maxOccurs=1 is the default always and means that in xml file tag/element can be there max once. in case of choice default is minOccurs="0" and maxOccurs="0".
matti
@matti Ok, sorry for confusing you. So if you want exactly 1 of dbtablename, and 1 of other two, you need not specify anything more.
Draco Ater
A: 

So, you want either fieldname or freetext and not both? or maybe both? and then dbtablename optionally?

Here is 1 or 2 of the elements:

<xs:choice minOccurs="1" maxOccurs="2">
    <xs:element name="fieldname" type="xs:string"/>
    <xs:element name="freetext" type="xs:string"/>
    <xs:element name="dbtablename" type="xs:string"/>
</xs:choice>

Is this what you want? or did you want dbtablename to be separate?

Jeff Walker
sorry: weekend happened: either fieldname or freetext. dbtablename always. the problem with choice is that xsd.exe generates weird code for it.
matti
yes: either fieldname or freetext and not both.
matti
what minOccurs="1" maxOccurs="2" mean here?
matti
Well, min/max on the choice means that you want either 1 or 2 of the elements. That isn't what you want and Draco's answer is closer to what you wanted. Most xsd compilers generate some strange code for choices. I use Jax-b 2 and it is acceptable to me.
Jeff Walker
thanks anyway! Jax-b 2 is for Java and I need C# class generator. Have to try WSCF (for WSDL->Classes) since it has .xsd generation option also.
matti