How to read the HTML DTD

Contents

1. DTD Comments

2. Parameter Entity definitions

3. Element declarations

    . Content model definitions

4. Attribute declarations

    . DTD entities in attribute definitions

    . Boolean attributes

Each element and attribute declaration in this specification is accompanied by its document type definition fragment. We have chosen to include the DTD fragments in the specification rather than seek a more approachable, but longer and less precise means of describing an element‘s properties. The following tutorial should allow readers unfamiliar with SGML to read the DTD and understand the technical details of the HTML specificationi.

1. DTD Comments

In DTDs, comments may spread over one or more lines. In the DTD, comments are delimited by a pair of "--" marks, e.g.

<!ELEMENT PARAM - O EMPTY    -- named property value -->

Here, the comment "named property value" explains the use of the PARAM element type. Comments in the DTD are informative only.

2. Parameter entity definitions

The HTML DTD begins with a series of parameter entity definitions. A parameter entity definition defines a kind of macro that be referenced and expanded elsewhere in the DTD. These macros may not appear in HTML documents, only in the DTD. Other types of macros, called character references, may be used in the text of an HTML document or within attribute values.

When the parameter entity is refered to by name in the DTD, it is expanded into a string.

A parameter entity definition begins with the keyword <!ENTITY % followed by the entity name, the quoted string the entity expands to, and finally a closing >. Instances of parameter entities in a DTD begin with "%", then the parameter entity name, and terminated by an optional ";".

The following example defines the string that the "%fontstyle;" entity will expand to.

  <!ENTITY % fontstyle "TT | I | B | BIG | SMALL">

The string the parameter entity expands to may contain other parameter entity names. These names are expanded recursively. In the following example, the "%inline;" parameter entity is defined to include the "%fontstyle;", "%phrase;", "%special;" and "%formctrl;" parameter entities.

  <!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">

You will encounter two DTD entities frequently in the HTML DTD: %block;" %inline;". They are used when the content model includes block-level and inline elements, respectively (defined in the section on the global structure of an HTML document).

3. ELement declarations

The bulk of the HTML DTD consists of the declarations of element types and their attributes. The <!ELEMENT keyword begins a declaration and the > character ends it. Between these are specified:

(1). The element‘s name.

(2). Whether the element‘s tags are optional. Two hyphens that appear after the element name mean that the       start and end tags are mandatory. One         hyphen followed by the letter "O" indicates that the end tag can be       omitted. A pair of letter "O"s indicated that both the start and end tags can be         omitted.

(3) The element‘s content, if any. The allowed content for an element is called its content model. Element types that are designed to have no content are          called empty elements. The content model for such element types is declared using the keyword "EMPTY".

In this example:

  <!ELEMENT UL - - (LI)+>

 . The element type being declared is UL.

. The two hyphens indicate that both the start tag <UL> and the end tag </UL> for this element type are          required.

. The content model for this element type is declared to be "at least one LI element". Below, we explain how

to specify content models.

This example illustrates the declaration of an empty element type:

  <!ELEMENT IMG - O EMPTY>

. The element type being declared is IMG.

 . The hyphen and the following "O" indicate that the end tag can be omitted, but together with the content

model "EMPTY", this is strengthened to the rule that the end tag must be omitted.

 . The "EMPTY" keyword means that instances of this type must not have content.

Content model definitions

The content model describes what may be contained by an instance of an element type. Content model definitions may include:

The names of allowed or forbidden element types (e.g., the UL element contains instances of the LI element type, and the P element type may not contain other P elements).
DTD entities (e.g., the LABEL element contains instances of the "%inline;" parameter entity).
Document text (indicated by the SGML construct "#PCDATA"). Text may contain character references. Recall that these begin with & and end with a semicolon (e.g., "Herg&eacute;‘s adventures of Tintin" contains the character entity reference for the "e acute" character).
The content model of an element is specified with the following syntax. Please note that the list below is a simplification of the full SGML syntax rules and does not address, e.g., precedences.

( ... )
Delimits a group.
A
A must occur, one time only.
A+
A must occur one or more times.
A?
A must occur zero or one time.
A*
A may occur zero or more times.
+(A)
A may occur.
-(A)
A must not occur.
A | B
Either A or B must occur, but not both.
A , B
Both A and B must occur, in that order.
A & B
Both A and B must occur, in any order.
Here are some examples from the HTML DTD:

<!ELEMENT UL - - (LI)+>
The UL element must contain one or more LI elements.

<!ELEMENT DL - - (DT|DD)+>
The DL element must contain one or more DT or DD elements in any order.

<!ELEMENT OPTION - O (#PCDATA)>
The OPTION element may only contain text and entities, such as &amp; -- this is indicated by the SGML data type #PCDATA.

A few HTML element types use an additional SGML feature to exclude elements from their content model. Excluded elements are preceded by a hyphen. Explicit exclusions override permitted elements.

In this example, the -(A) signifies that the element A cannot appear in another A element (i.e., anchors may not be nested).

<!ELEMENT A - - (%inline;)* -(A)>
Note that the A element type is part of the DTD parameter entity "%inline;", but is excluded explicitly because of -(A).

Similarly, the following element type declaration for FORM prohibits nested forms:

<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM)>

4. Attribute declarations

The <!ATTLIST keyword begins the declaration of attributes that an element may take. It is followed by the name of the element in question, a list of attribute definitions, and a closing >. Each attribute definition is a triplet that defines:

. The name of an attribute.
. The type of the attribute‘s value or an explicit set of possible values. Values defined explicitly by the DTD are case-insensitive. Please consult the section on   basic HTML data types for more information about attribute value types.
. Whether the default value of the attribute is implicit (keyword "#IMPLIED"), in which case the default value must be supplied by the user agent (in some       cases via inheritance from parent elements); always required (keyword "#REQUIRED"); or fixed to the given value (keyword "#FIXED"). Some attribute         definitions explicitly specify a default value for the attribute.
In this example, the name attribute is defined for the MAP element. The attribute is optional for this element.

<!ATTLIST MAP
name CDATA #IMPLIED
>
The type of values permitted for the attribute is given as CDATA, an SGML data type. CDATA is text that may contain character references.

For more information about "CDATA", "NAME", "ID", and other data types, please consult the section on HTML data types.

The following examples illustrate several attribute definitions:

rowspan NUMBER 1 -- number of rows spanned by cell --
http-equiv NAME #IMPLIED -- HTTP response header name --
id ID #IMPLIED -- document-wide unique id --
valign (top|middle|bottom|baseline) #IMPLIED
The rowspan attribute requires values of type NUMBER. The default value is given explicitly as "1". The optional http-equiv attribute requires values of type NAME. The optional id attribute requires values of type ID. The optional valign attribute is constrained to take values from the set {top, middle, bottom, baseline}.

DTD entities in attribute definitions

Attribute definitions may also contain parameter entity references.

In this example, we see that the attribute definition list for the LINK element begins with the "%attrs;" parameter entity.

<!ELEMENT LINK - O EMPTY -- a media-independent link -->
<!ATTLIST LINK
%attrs; -- %coreattrs, %i18n, %events --
charset %Charset; #IMPLIED -- char encoding of linked resource --
href %URI; #IMPLIED -- URI for linked resource --
hreflang %LanguageCode; #IMPLIED -- language code --
type %ContentType; #IMPLIED -- advisory content type --
rel %LinkTypes; #IMPLIED -- forward link types --
rev %LinkTypes; #IMPLIED -- reverse link types --
media %MediaDesc; #IMPLIED -- for rendering on these media --
>
Start tag: required, End tag: forbidden

The "%attrs;" parameter entity is defined as follows:

<!ENTITY % attrs "%coreattrs; %i18n; %events;">
The "%coreattrs;" parameter entity in the "%attrs;" definition expands as follows:

<!ENTITY % coreattrs
"id ID #IMPLIED -- document-wide unique id --
class CDATA #IMPLIED -- space-separated list of classes --
style %StyleSheet; #IMPLIED -- associated style info --
title %Text; #IMPLIED -- advisory title --"
>
The "%attrs;" parameter entity has been defined for convenience since these attributes are defined for most HTML element types.

Similarly, the DTD defines the "%URI;" parameter entity as expanding into the string "CDATA".

<!ENTITY % URI "CDATA"
-- a Uniform Resource Identifier,
see [URI]
-->
As this example illustrates, the parameter entity "%URI;" provides readers of the DTD with more information as to the type of data expected for an attribute. Similar entities have been defined for "%Color;", "%Charset;", "%Length;", "%Pixels;", etc.

Boolean attributes

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").

This example defines the selected attribute to be a boolean attribute.

selected (selected) #IMPLIED -- option is pre-selected --
The attribute is set to "true" by appearing in the element‘s start tag:

<OPTION selected="selected">
...contents...
</OPTION>
In HTML, boolean attributes may appear in minimized form -- the attribute‘s value appears alone in the element‘s start tag. Thus, selected may be set by writing:

<OPTION selected>
instead of:

<OPTION selected="selected">
Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.

时间: 2024-10-22 13:27:37

How to read the HTML DTD的相关文章

XML的DTD和Schema约束

为什么要使用约束? XML是自定义的标签,有时候标签太多,记不住,所以就需要有约束来告诉我能写哪些标签,哪些标签写错了不能识别 XML中有哪几种约束? 有很多约束,其中DTD和Schema约束最为常见. 约束本质上是什么? 约束本质上也是一种xml文件. DTD约束和Schema约束的区别 特点的区别: DTD约束较为古老,简单,一些老框架使用DTD作为约束:Struts2和Hibernate都使用DTD作为其XML配置文件的约束 Schema约束功能更为强大,用的更为广泛,Tomcat和Spr

Myeclipse中struts2配置文件配置dtd以支持自动补全

如果在eclipse中配置只需要,只需要在菜单栏window->preference->Myeclipse->file and eiditor->xml->xml catalog->add添加: Location中选中struts-2.3.dtd所在位置如下图所示 Key Type选中URI Key:在struts.xml中有   <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD St

【超实用】图解--如何使用本地的dtd文件映射

以前一直很苦恼,如果电脑上不了网,就比较麻烦了,自己在配置HIbernate的属性的时候,不知道属性名有没有写错.. 现在和大家分享一下,毕竟自己痛苦过了,大家不要和我一样痛苦. [超实用]图解--如何使用本地的dtd文件映射,布布扣,bubuko.com

DTD验证XML文档

DTD验证XML文档        1.DTD简介:DTD是Document Type Definition的缩写,即文档定义            1.1:DTD的内容包含:                    元素定义规则                    元素之间的关系规则                    属性的定义规则            1.2:DTD的作用如下:                    DTD使每个XML文件可以携带一个自身格式的描述          

Referenced file contains errors (http://tiles.apache.org/dtds/tiles-config_3_0.dtd)

java开发时遇到的问题,之前还是好好的,没有错误提示.可是今天一打开项目就出现这种问题.真不知道是怎么回事,在这里求助.错误如下: Referenced file contains errors (http://tiles.apache.org/dtds/tiles-config_3_0.dtd). For more information, right click on the message in the Problems View and select "Show Details...&

DTD与XML基本语法规则

DTD(文档类型定义)可以定义合法的XML文档结构,它使用一系列合法元素来定义文档的结构.DTD分为内部DTD和外部DTD,所谓内部DTD是指该DTD在某个文档的内部,只被该文档使用.外部DTD是指该DTD不在文档内部,可以被其他所有的文档来共享.DTD文档与XML文档实例的关系可以看成是类和对象的关系. (1)外部DTD文件的编写及引用 新建一个外部family.dtd文件 <!ELEMENT family (father,mother,son+)> <!ELEMENT father 

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;详解

每次写html页面开头基本都会加上这么两行: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> ************************

二、XML的DTD和Schema

  1.DTD简单介绍   2.schema入门 schema出现的目的是通过一个更加合理的方式来编写xml的限制文件(基于xml语法的方式) schema可以使用命名空间来支持多个名称相同的元素 schema可以很好的完成对java或者所有对象的修饰并且提供了大量的数据类型     2.1.命名空间 在xml中引入schema 通过文件路径引入     2.2.元素和属性     2.3.相关约束   3.深入schema     3.1.数据模型的创建     3.2.相关的设计方案    

DTD文档类型定义文件简介

dtd 基本概念: dtd ( document type definition  文档类型定义),该文件一般和xml文件配合使用, 主要的用处是约束 xml. 除了 dtd 技术外, 还有一个schema的技术也可以用于约束xml文件的书写规范. 现在请看一个问题: <stu id="a"0&apos;0&apos;1<" > <name>杨过</name> <sex>男</sex> <

死磕dtd(1)

看到安卓开发里大量的xml文件和layout里的Android UI开始复习一下xml xml的校验规则依据dtd dtd里面大小写敏感.....查找了好久才发现这个问题 <?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <