document type definition - dtd criando documentos xml válidos

22
Document Type Definition - DTD Criando Documentos XML “Válidos”

Upload: internet

Post on 18-Apr-2015

119 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Document Type Definition - DTD Criando Documentos XML Válidos

Document Type Definition - DTD

Criando Documentos XML “Válidos”

Page 2: Document Type Definition - DTD Criando Documentos XML Válidos

Introdução

• Um documento XML é válido somente se este:– possui um DTD associado a ele – está de acordo com com as restrições impostas

no DTD

Page 3: Document Type Definition - DTD Criando Documentos XML Válidos

Introdução

• O DTD deve aparecer antes do primeiro elemento em um documento XML ou como um documento a parte.

• O nome após a etiqueta DOCTYPE no DTD deve ser o nome da raiz do documento XML

Page 4: Document Type Definition - DTD Criando Documentos XML Válidos

Introdução

• Exemplo: Um documento XML pode conter apenas o elemento raiz tutorial contendo algum texto:

<?xml version='1.0' encoding='ISO-8859-1'?>

<!DOCTYPE tutorial [

<!ELEMENT tutorial (#PCDATA)>]>

<tutorial> Tutorial de DTD </tutorial>

Page 5: Document Type Definition - DTD Criando Documentos XML Válidos

Introdução

<?xml version='1.0' encoding='ISO-8859-1'?>

<!DOCTYPE tutorial [<!ELEMENT tutorial (AAA , BBB)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)>]> <tutorial> <AAA/><BBB/></tutorial>

Page 6: Document Type Definition - DTD Criando Documentos XML Válidos

Elementos aninhados

• Zero ou mais elementos AAA seguidos de um único elemento BBB:

<!ELEMENT tutorial (AAA* , BBB)>

• Um ou mais elementos AAA seguidos de um único elemento BBB:

<!ELEMENT tutorial (AAA+ , BBB)>

• AAA opcional:<!ELEMENT tutorial (AAA? , BBB)>

Page 7: Document Type Definition - DTD Criando Documentos XML Válidos

Exercício

• Construa três exemplos válidos e três inválidos de documentos XML baseados no DTD abaixo:

<!ELEMENT XXX (AAA? , BBB+)>

<!ELEMENT AAA (CCC? , DDD*)>

<!ELEMENT BBB (CCC , DDD)>

<!ELEMENT CCC (#PCDATA)>

<!ELEMENT DDD (#PCDATA)>

Page 8: Document Type Definition - DTD Criando Documentos XML Válidos

Seleção “|”

<!ELEMENT XXX (AAA , BBB)>

<!ELEMENT AAA (CCC , DDD)>

<!ELEMENT BBB (CCC | DDD)>

<!ELEMENT CCC (#PCDATA)>

<!ELEMENT DDD (#PCDATA)>

Page 9: Document Type Definition - DTD Criando Documentos XML Válidos

Intercalação de texto e elementos

• O elemento BBB pode conter qualquer combinação de texto e do elemento CCC:

<!ELEMENT tutorial (AAA* , BBB)>

<!ELEMENT XXX (AAA+ , BBB+)>

<!ELEMENT AAA (BBB | CCC )>

<!ELEMENT BBB (#PCDATA | CCC )*>

<!ELEMENT CCC (#PCDATA)>

Page 10: Document Type Definition - DTD Criando Documentos XML Válidos

Exercício

• Construa um XML válido para o DTD:<!ELEMENT tutorial (AAA* , BBB)>

<!ELEMENT XXX (AAA+ , BBB+)>

<!ELEMENT AAA (BBB | CCC )>

<!ELEMENT BBB (#PCDATA | CCC )*>

<!ELEMENT CCC (#PCDATA)>

Page 11: Document Type Definition - DTD Criando Documentos XML Válidos

Atributos

• Servem para atribuir pares nome-valor com elementos.

• Atributos só podem aparecer em etiquetas de início e em etiquetas vazias.

• A declaração de atributos inicia com o nome ATTLIST, seguido do nome do elemento e da lista de seus atributos.

Page 12: Document Type Definition - DTD Criando Documentos XML Válidos

Atributos

<!ELEMENT attributes (#PCDATA)>

<!ATTLIST attributes

aaa CDATA #REQUIRED

bbb CDATA #IMPLIED>

A ordem dos atributos não importa

Atributos CDATA: permite qualquer caracter de acordo com as regras de boa formação

#REQUIRED: obrigatório

#IMPLIED: Opcional

Page 13: Document Type Definition - DTD Criando Documentos XML Válidos

NMTOKEN(S)

• NMTOKEN: letras, dígitos, ponto [ . ] , hífen [ - ], underline [ _ ] dois pontos [ : ] .

• NMTOKENS: NMTOKEN mais o espaço em branco: um ou mais espaços em branco, return, fim de linha e tabulações.

<!ELEMENT attributes (#PCDATA)>

<!ATTLIST attributes

aaa CDATA #IMPLIED

bbb NMTOKEN #REQUIRED

ccc NMTOKENS #REQUIRED>

Page 14: Document Type Definition - DTD Criando Documentos XML Válidos

Tipo ID

• Um atributo do tipo ID pode conter apenas caracteres permitidos pelo tipo NMTOKEN e devem iniciar por uma letra.

• Nenhum tipo de elemento deve conter mais de um atributo do tipo ID

• O valor de um atributo do tipo ID deve ser único dentre todos atributos ID das instâncias do elemento.

Page 15: Document Type Definition - DTD Criando Documentos XML Válidos

Tipo ID

<!ELEMENT XXX (AAA+ , BBB+ , CCC+)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)><!ELEMENT CCC (#PCDATA)><!ATTLIST AAA id ID #REQUIRED><!ATTLIST BBB code ID #IMPLIED list NMTOKEN #IMPLIED> <!ATTLIST CCC X ID #REQUIRED Y NMTOKEN #IMPLIED>

Page 16: Document Type Definition - DTD Criando Documentos XML Válidos

IDREF • Um atributo IDREF deve referencia algum ID

existente no documento. IDREFS referencia um seqüência de IDs

<!ELEMENT XXX (AAA+ , BBB+, CCC+, DDD+)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)><!ELEMENT CCC (#PCDATA)><!ELEMENT DDD (#PCDATA)><!ATTLIST AAA mark ID #REQUIRED><!ATTLIST BBB id ID #REQUIRED> <!ATTLIST CCC ref IDREF #REQUIRED><!ATTLIST DDD ref IDREFS #REQUIRED>

Page 17: Document Type Definition - DTD Criando Documentos XML Válidos

IDREF - Exemplo

<XXX>

<AAA mark="a1"/>

<AAA mark="a2"/>

<AAA mark="a3"/>

<BBB id="b001" />

<CCC ref="a3" />

<DDD ref="a1 b001 a2" />

</XXX>

Page 18: Document Type Definition - DTD Criando Documentos XML Válidos

Especificando valores de atributo permitidos

<!ELEMENT XXX (AAA+, BBB+)>

<!ELEMENT AAA (#PCDATA)>

<!ELEMENT BBB (#PCDATA)>

<!ATTLIST AAA

true ( yes | no ) #REQUIRED>

<!ATTLIST BBB

month (1|2|3|4|5|6|7|8|9|10|11|12) #IMPLIED>

Page 19: Document Type Definition - DTD Criando Documentos XML Válidos

Valores permitidos - Exemplo

<XXX>

<AAA true="yes"/>

<AAA true="no"/>

<AAA true="yes"/>

<BBB month="8" />

<BBB month="2" />

<BBB month="12" />

</XXX>

Page 20: Document Type Definition - DTD Criando Documentos XML Válidos

Valor Default

<!ATTLIST AAA

true ( yes | no ) "yes">

Page 21: Document Type Definition - DTD Criando Documentos XML Válidos

Elemento só com atributos, sem texto (Vazio)

<!ELEMENT AAA EMPTY>

Exemplo:

<XXX>

<AAA true="yes"/>

<AAA true="no"></AAA>

</XXX>

Page 22: Document Type Definition - DTD Criando Documentos XML Válidos