W3C home > Mailing lists > Public > xmlschema-dev@w3.org > December 2001

Re: How to make XML Tags Optional

From: Jeni Tennison <jeni@jenitennison.com>
Date: Fri, 14 Dec 2001 11:26:45 +0000
Message-ID: <16463632278.20011214112645@jenitennison.com>
To: Prasanna Tadinada <Prasanna.Tadinada@Aceva.com>
CC: "'xmlschema-dev@w3.org'" <xmlschema-dev@w3.org>
Hi Prasanna,

> Say this is my XML File
>         <system_header>
>                 <trace_key>gsfgsrgwrw</trace_key>
>                 <originator>
>                         <domain>example</domain>
>                         <system_name>domain</system_name>
>                 </originator>
>                 <create_time>2000-05-24T163330Z</create_time>
>         </system_header>
> How can i make the Tag <trace_key>gsfgsrgwrw</trace_key> as Optional
> Tag and the Value Optional
> How to do it if the tag is <originator>, does the children are to be
> defined optional and nullable.

You can make the trace_key element optional by setting the minOccurs
attribute on the xs:element that declares/refers to it to 0:

<xs:element name="system_header">
      <xs:element name="trace_key" type="xs:string" minOccurs="0" />

Assuming that the rest of the content model allows originator and
create_time elements, that would allow:


There are several ways you could make the trace_key element accept an
empty value.

If you set it to a type that can have an empty string as a lexical
value (for example xs:string, xs:normalizedString, xs:token) then it
happens automatically.

If the trace_key value is of a type where an empty string isn't a
valid value, then you can allow an empty string by creating a union of
the type of the trace_key and a restriction of xs:string which has a
single enumerated value (an empty string). For example, if trace_key
contains values of type xs:NMTOKEN then you could do:

  <xs:element name="trace_key">
      <xs:union memberTypes="xs:NMTOKEN">
          <xs:restriction base="xs:string">
            <xs:enumeration value="" />

This would allow the following:


Alternatively, as you suggest, you could use nillable to allow
trace_key to take a nil value. It's only worth doing if trace_key
is of a type for which an empty string is not a legal value, or if you
are using the PSVI and want to have nil values flagged as nil values.
If you have trace_key being a name token, for example:

  <xs:element name="trace_key" type="xs:NMTOKEN" nillable="true" />

Then the following is valid:

    <trace_key xsi:nil="true"></trace_key>

But the following is invalid:


Whereas if you have trace_key being a string:

  <xs:element name="trace_key" type="xs:string" nillable="true" />

Then both the instances above are valid, but in the first the 'nil'
flag is true for the trace_key element.



Jeni Tennison
Received on Friday, 14 December 2001 06:26:55 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:14:56 UTC