Στοιχείο attribute XML Schema

Ορισμός και χρήση

Το στοιχείο attribute ορίζει μια ιδιότητα.

Στοιχείο πληροφοριών

Περίοδος εμφάνισης Ορίζεται μια φορά στο στοιχείο schema. Αναφέρεται πολλές φορές σε τύπους ή ομάδες ιδιοτήτων.
Γονέας στοιχείο attributeGroup, schema, complexType, restriction (simpleContent), extension (simpleContent), restriction (complexContent), extension (complexContent)
Περιεχόμενο annotation, simpleType

Γραμματική

<attribute
default=string
fixed=string
form=qualified|unqualified
id=ID
name=NCName
ref=QName
type=QName
use=optional|prohibited|required
any attributes
>
(annotation?,(simpleType?))
</attribute>

(? Σύμβολο που δήλωνε ότι το στοιχείο μπορεί να εμφανιστεί στο στοιχείο attribute μηδενικές ή μια φορά.)

Ιδιότητα

default

Επιλογή. Καθορίζει την προεπιλεγμένη τιμή της ιδιότητας. Οι ιδιότητες default και fixed δεν μπορούν να εμφανίζονται ταυτόχρονα.

fixed

Επιλογή. Καθορίζει τη σταθερή τιμή της ιδιότητας. Οι ιδιότητες default και fixed δεν μπορούν να εμφανίζονται ταυτόχρονα.

form

Επιλογή. Καθορίζει τη μορφή της ιδιότητας. Η προεπιλεγμένη τιμή είναι η τιμή της ιδιότητας attributeFormDefault του στοιχείου schema. Μπορεί να οριστεί ως εξής:

  • “qualified” - Ενδείκνυε ότι η ιδιότητα πρέπει να καθοριστεί με πρόθεμα ονομαστικού χώρου και το μη-παράδειγμα του ονόματος (NCName) χωρίς πρόθεμα.
  • "unqualified" - Indicates that this attribute does not need to be prefixed by a namespace, and does not need to match the attribute's non-colon name (NCName), that is, the local name.

id

Optional. Specify a unique ID for the element.

name

Optional. Specify the name of the attribute. The name and ref attributes cannot appear together.

ref

Optional. Specify a reference to the specified attribute. The name and ref attributes cannot appear together. If ref appears, the simpleType element, form, and type cannot appear.

type

Optional. Specify built-in data types or simple types. The type attribute can only appear when the content does not contain a simpleType element.

use

Optional. Specify how to use this attribute. The following values can be set:

  • optional - The attribute is optional and can have any value (default).
  • prohibited - The attribute cannot be used.
  • required - The attribute is required.

any attributes

Optional. Specify any other attributes with a non-schema namespace.

Instance

Example 1

<xs:attribute name="code">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[A-Z][A-Z]"/>
  </xs:restriction>
</xs:simpleType>
</xs:attribute>

The above example indicates that the "code" attribute has a restriction. The only acceptable values are two letters from the uppercase letters A to Z.

Example 2

To declare an attribute using an existing attribute definition within a complex type, use the ref attribute:

<xs:attribute name="code">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z][A-Z]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>
<xs:complexType name="someComplexType">
  <xs:attribute ref="code"/>
</xs:complexType>

Example 3

The attribute can have both a default value and a specified fixed value. When no other value is specified, the default value is automatically assigned to the attribute. In the following example, the default value is "EN":

<xs:attribute name="lang" type="xs:string" default="EN"/>

When no other value is specified, the attribute is automatically assigned a fixed value. However, unlike the default value, if you specify a value other than the fixed value for the attribute, the document will validate it as invalid. In the following example, the fixed value is "EN":

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

Παράδειγμα 4

Όλες οι ιδιότητες είναι προεπιλεγμένες ως προαιρετικές. Για να καθορίσετε σαφώς ότι η ιδιότητα είναι προαιρετική, χρησιμοποιήστε την ιδιότητα "use":

<xs:attribute name="lang" type="xs:string" use="optional"/>

Καθορισμός της ιδιότητας ως υποχρεωτική:

<xs:attribute name="lang" type="xs:string" use="required"/>