DTD - Atributos
Em um DTD, os atributos são declarados com uma declaração ATTLIST.
Declarando Atributos
Uma declaração de atributo tem a seguinte sintaxe:
<!ATTLIST element-name attribute-name attribute-type attribute-value>
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" />
O tipo de atributo pode ser um dos seguintes:
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 |
O valor do atributo pode ser um dos seguintes:
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 |
Um valor de atributo padrão
DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />
No exemplo acima, o elemento "square" é definido como um elemento vazio com um atributo "width" do tipo CDATA. Se nenhuma largura for especificada, ela terá um valor padrão de 0.
#REQUERIDOS
Sintaxe
<!ATTLIST element-name attribute-name attribute-type #REQUIRED>
Exemplo
DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:
<person />
Use a palavra-chave #REQUIRED se você não tiver uma opção para um valor padrão, mas ainda quiser forçar a presença do atributo.
#IMPLÍCITA
Sintaxe
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
Exemplo
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />
Use a palavra-chave #IMPLIED se não quiser forçar o autor a incluir um atributo e não tiver uma opção para um valor padrão.
#FIXO
Sintaxe
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
Exemplo
DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />
Use a palavra-chave #FIXED quando quiser que um atributo tenha um valor fixo sem permitir que o autor o altere. Se um autor incluir outro valor, o analisador XML retornará um erro.
Valores de atributo enumerados
Sintaxe
<!ATTLIST element-name attribute-name (en1|en2|..) default-value>
Exemplo
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
Use valores de atributo enumerados quando desejar que o valor do atributo seja um de um conjunto fixo de valores legais.