Re: How to make XML Tags Optional

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:complexType>
    <xs:sequence>
      <xs:element name="trace_key" type="xs:string" minOccurs="0" />
      ...
    </xs:sequence>
  </xs:complexType>
</xs:element>

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

  <system_header>
    <originator>
      <domain>example</domain>
      <system_name>domain</system_name>
    </originator>
    <create_time>2000-05-24T163330Z</create_time>
  </system_header>

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:simpleType>
      <xs:union memberTypes="xs:NMTOKEN">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="" />
          </xs:restriction>
        </xs:simpleType>
      </xs:union>
    </xs:simpleType>
  </xs:element>

This would allow the following:

  <system_header>
    <trace_key></trace_key>
    ...
  </system_header>

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:

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

But the following is invalid:

  <system_header>
    <trace_key></trace_key>
    ...
  </system_header>

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.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

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