DTD - Elements

In a DTD, elements are declared with an ELEMENT declaration.

Declaring Elements

In a DTD, XML elements are declared with an element declaration with the following syntax:

<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>

Empty Elements

Empty elements are declared with the category keyword EMPTY:

<!ELEMENT element-name EMPTY>

Example:

<!ELEMENT br EMPTY>

XML example:

<br />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE br [
<!ELEMENT br EMPTY>
]>
<br/>

Elements with Parsed Character Data

Elements with only parsed character data are declared with #PCDATA inside

<!ELEMENT element-name (#PCDATA)>

Example:

<!ELEMENT from (#PCDATA)>

Elements with any Contents

Elements declared with the category keyword ANY, can contain any combination of parsable data:

<!ELEMENT element-name ANY>

Example:

<!ELEMENT note ANY>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE content [
<!ELEMENT content (any+)>
<!ELEMENT any ANY>
]>
<content>
    <any></any>
    <any>
        <any></any>
    </any>
    <any>&lt;</any>
</content>

Elements with Children (sequences)

Elements with one or more children are declared with the name of the children elements inside parentheses:

<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>

Example:

<!ELEMENT note (to,from,heading,body)>

When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared, and the children can also have children. The full declaration of the "note" element is:

<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Declaring Only One Occurrence of an Element

<!ELEMENT element-name (child-name)>

Example:

<!ELEMENT note (message)>

The example above declares that the child element "message" must occur once, and only once inside the "note" element.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (message)>
<!ELEMENT message (#PCDATA)>
]>
<note>
    <message></message>
</note>

Declaring Minimum One Occurrence of an Element

<!ELEMENT element-name (child-name+)>

Example:

<!ELEMENT note (message+)>

The + sign in the example above declares that the child element "message" must occur one or more times inside the "note" element.

Declaring Zero or More Occurrences of an Element

<!ELEMENT element-name (child-name*)>

Example:

<!ELEMENT note (message*)>

The * sign in the example above declares that the child element "message" can occur zero or more times inside the "note" element.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (message*)>
<!ELEMENT message (#PCDATA)>
]>
<note>

</note>

Declaring Zero or One Occurrences of an Element

<!ELEMENT element-name (child-name?)>

Example:

<!ELEMENT note (message?)>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (message?)>
<!ELEMENT message (#PCDATA)>
]>
<note>
<message></message>
</note>

The ? sign in the example above declares that the child element "message" can occur zero or one time inside the "note" element.

Declaring either/or Content

Example:

<!ELEMENT note (to,from,header,(message|body))>

The example above declares that the "note" element must contain a "to" element, a "from" element, a "header" element, and either a "message" or a "body" element.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,header,(message|body))>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT header (#PCDATA)>
<!ELEMENT message (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
    <to></to>
    <from></from>
    <header></header>
    <body></body>
</note>

Declaring Mixed Content

Example:

<!ELEMENT note (#PCDATA|to|from|header|message)*>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ELEMENT note (#PCDATA|to|from|header|message)*>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT header (#PCDATA)>
<!ELEMENT message (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
    <to></to>
</note>

The example above declares that the "note" element can contain zero or more occurrences of parsed character data, "to", "from", "header", or "message" elements.

时间: 2024-10-13 23:10:09

DTD - Elements的相关文章

xml初步,DTD和Schema约束

XML 可扩展的标记语言(!!!可扩展) 作用 1.存放数据 2.配置文件 语法 文档声明 <?xml version="1.0" encoding="UTF-8"?> 1.文档声明必须为<?xml开头,以?>结束: 2.文档声明必须从文档的0行0列位置开始: 3.文档声明只有2个属性: version 版本 encoding 编码 元素 <bean></bean> 1.必须开始结束 2.标签由开始标签,元素体,结束标

DTD Tutorial

The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML document. A DTD defines the document structure with a list of legal elements and attributes.

DTD - XML Building Blocks

The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Documents Seen from a DTD point of view(从dtd的角度来看), all XML documents (and HTML documents) are made up by the following building blocks: Elements Attribu

Introduction to DTD

A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes. A DTD can be declared inline inside an XML document, or as an external reference. I

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 specifi

Hibernate的dtd文件和properties文件

hibernate-configuration-3.0.dtd 1 <!-- Hibernate file-based configuration document. 2 3 <!DOCTYPE hibernate-configuration PUBLIC 4 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 5 "http://www.hibernate.org/dtd/hibernate-configurat

无废话XML--XML约束(DTD)

基本术语     一.序言Prolog:包括XML声明(XML Declaration)和文档类型声明(Document Type Declaration). 二.良构(well-formed 规范的):符合W3C定义的XML文档. 为什么XML需要验证?    对XML文件施加额外的约束,以便交流.举个例子:要是我和你在交互一份数据,那么按照我的主管意识我可能会这样子定义,按照你的主观意识你可能会那样子定义,那么我们之间要怎么交互数据呢?没有一个提前规定号的约定来规范我们如何写XML,那么就连

DTD -- XML验证

DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块. DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. DTD简介 内部的 DOCTYPE 声明 假如 DTD 被包含在您的 XML 源文件中,它应当通过下面的语法包装在一个 DOCTYPE 声明中: <?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT

xhtml1-strict.dtd

<!-- Extensible HTML version 1.0 Strict DTD This is the same as HTML 4 Strict except for changes due to the differences between XML and SGML. Namespace = http://www.w3.org/1999/xhtml For further information, see: http://www.w3.org/TR/xhtml1 Copyright