Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify in an XML schema that either one of two fields must be present?

Tags:

xsd

I want to specify that either fieldname or freetext must always be present in XML files that apply to this XSD. Is there a way to do that?

<xs:complexType name="tSome">
<xs:sequence>
  <!-- either one of the two below has to be present. -->
  <xs:element name="fieldname" type="xs:string" />
  <xs:element name="freetext" type="xs:string" />
  <!-- this one below must always be present -->
  <xs:element name="dbtablename" type="xs:string" />
</xs:sequence>
</xs:complexType>
like image 263
char m Avatar asked Mar 26 '10 13:03

char m


People also ask

How do you make a field mandatory in XML Schema?

The "use" property in the XSD definition is used to specify if the attribute is optional or mandatory. To specify that an attribute must be present, use="required" (Note: use may also be set to "prohibited", but we'll come to that later).

How do you specify the schema of XML file?

The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against a schema. The line: xsi:noNamespaceSchemaLocation="shiporder. xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder. xml").

Which schema tag allows to specify elements in any order in XML?

xs:all specifies that the child elements can appear in any order.

What XML Schema type can be used to contain other elements and attributes?

A complex element is an XML element that contains other elements and/or attributes.

Can we define a data type for an element in XML Schema?

It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself. You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.


2 Answers

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

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

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

Edited: I didn't correctly understand the question the first time. If you want dbtablename to always be present with any one of 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>
like image 150
Draco Ater Avatar answered Sep 22 '22 18:09

Draco Ater


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?

like image 44
Jeff Walker Avatar answered Sep 19 '22 18:09

Jeff Walker