Re: optional attribute

Hi Hans,

> then Spy (version 4.4) tells me that:
>
> * I MUST fill in a string in the 'optional-attribute', which doesn't
> make sense to me, because that means that my application has to
> remove all non-filled-in optional attributes from the XML document
> (then Spy accepts the file as valid)

An optional attribute is an attribute that might or might not *exist*,
not one that might or might not have a value. If an optional attribute
is present on an element, then it must have a value that adheres to
the type of the attribute. In this case, it's valid for attributes
(and elements) of type xs:string to have an empty value, so the
following should all be valid with the schema you showed:

---
<test-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="D:\XML\test-of-optionality.xsd"
  optional-attribute=""
  required-attribute="test" />
---
<test-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="D:\XML\test-of-optionality.xsd"
  optional-attribute="test"
  required-attribute="test" />
---
<test-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="D:\XML\test-of-optionality.xsd"
  required-attribute="test" />
---
<test-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="D:\XML\test-of-optionality.xsd"
  required-attribute="" />
---

On the other hand, if you'd declared that the optional-attribute and
required-attribute had a type of xs:integer (for which empty values
aren't valid) then, if present, the optional-attribute would have to
have a value. In other words, the following would be valid:

---
<test-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="D:\XML\test-of-optionality.xsd"
  optional-attribute="1"
  required-attribute="2" />
---
<test-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="D:\XML\test-of-optionality.xsd"
  required-attribute="1" />
---

and the following wouldn't be valid:

---
<test-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="D:\XML\test-of-optionality.xsd"
  optional-attribute=""
  required-attribute="1" />
---

If you want to declare an attribute that has to be present, but may or
may not have a value, and the type for the attribute must be an
xs:integer (say), then you need to declare a type for the attribute
that's a union of the type that you want for the attribute (e.g.
xs:integer) and an empty string, as in:

  <xs:attribute name="attribute-with-optional-value">
    <xs:simpleType>
      <xs:union memberTypes="xs:integer">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="" />
          </xs:restriction>
        </xs:simpleType>
      </xs:union>
    </xs:simpleType>
  </xs:attribute>

> * I may not fill in a string in the 'prohibited-attribute', which
> makes sense

Note that "prohibited" attributes are only useful when doing
derivation by restriction from a type that allows that attribute
specifically (as an optional attribute); it makes no sense to define a
prohibited attribute within a complex type that's derived from
xs:anyType.

Cheers,

Jeni

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

Received on Tuesday, 18 November 2003 11:18:30 UTC