DTD - Attributi
In un DTD, gli attributi sono dichiarati con una dichiarazione ATTLIST.
Attributi di dichiarazione
Una dichiarazione di attributo ha la seguente sintassi:
<!ATTLIST element-name attribute-name attribute-type attribute-value>
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" />
Il tipo di attributo può essere uno dei seguenti:
Type | Description |
---|---|
CDATA | The value is character data |
(en1|en2|..) | The value must be one from an enumerated list |
ID | The value is a unique id |
IDREF | The value is the id of another element |
IDREFS | The value is a list of other ids |
NMTOKEN | The value is a valid XML name |
NMTOKENS | The value is a list of valid XML names |
ENTITY | The value is an entity |
ENTITIES | The value is a list of entities |
NOTATION | The value is a name of a notation |
xml: | The value is a predefined xml value |
Il valore dell'attributo può essere uno dei seguenti:
Value | Explanation |
---|---|
value | The default value of the attribute |
#REQUIRED | The attribute is required |
#IMPLIED | The attribute is optional |
#FIXED value | The attribute value is fixed |
Un valore di attributo predefinito
DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
Nell'esempio sopra, l'elemento "quadrato" è definito come un elemento vuoto con un attributo "larghezza" di tipo CDATA. Se non viene specificata alcuna larghezza, ha un valore predefinito di 0.
#NECESSARIO
Sintassi
<!ATTLIST element-name attribute-name attribute-type #REQUIRED>
Esempio
DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:
<person />
Utilizza la parola chiave #REQUIRED se non hai un'opzione per un valore predefinito, ma vuoi comunque forzare la presenza dell'attributo.
#IMPLICITO
Sintassi
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
Esempio
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
Usa la parola chiave #IMPLIED se non vuoi forzare l'autore a includere un attributo e non hai un'opzione per un valore predefinito.
#FISSO
Sintassi
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
Esempio
DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />
Usa la parola chiave #FIXED quando vuoi che un attributo abbia un valore fisso senza che l'autore lo modifichi. Se un autore include un altro valore, il parser XML restituirà un errore.
Valori di attributo enumerati
Sintassi
<!ATTLIST element-name attribute-name (en1|en2|..) default-value>
Esempio
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
Utilizzare i valori di attributo enumerati quando si desidera che il valore dell'attributo sia uno di un insieme fisso di valori legali.