In a DTD, attributes are declared with an ATTLIST declaration.

Declaring Attributes

An attribute declaration has the following syntax:

<!ATTLIST element-name attribute-name attribute-type attribute-value>

DTD example:

<!ATTLIST payment type CDATA "check">

XML example:

<payment type="check" />

The attribute-type can be one of the following:

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

The attribute-value can be one of the following:

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

A Default Attribute Value

DTD:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0"> Valid XML:
<square width="100" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE square [
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
]>
<square width="100"/>

In the example above, the "square" element is defined to be an empty element with a "width" attribute of  type CDATA. If no width is specified, it has a default value of 0.

#REQUIRED

Syntax

<!ATTLIST element-name attribute-name attribute-type #REQUIRED>

Example

DTD:
<!ATTLIST person number CDATA #REQUIRED> Valid XML:
<person number="5677" /> Invalid XML:
<person />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE person [
<!ELEMENT person EMPTY>
<!ATTLIST person number CDATA #REQUIRED>
]>
<person number="5677"/>

Use the #REQUIRED keyword if you don't have an option for a default value, but still want to force the attribute to be present.

#IMPLIED

Syntax

<!ATTLIST element-name attribute-name attribute-type #IMPLIED>

Example

DTD:
<!ATTLIST contact fax CDATA #IMPLIED> Valid XML:
<contact fax="555-667788" /> Valid XML:
<contact />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE contact [
<!ELEMENT contact EMPTY>
<!ATTLIST contact fax CDATA #IMPLIED>
]>
<contact fax="555-667788"/>

Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't have an option for a default value.

#FIXED

Syntax

<!ATTLIST element-name attribute-name attribute-type #FIXED "value">

Example

DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft"> Valid XML:
<sender company="Microsoft" /> Invalid XML:
<sender company="W3Schools" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sender [
<!ELEMENT sender EMPTY>
<!ATTLIST sender company CDATA #FIXED "Microsoft">
]>
<sender company="Microsoft"/>

Use the #FIXED keyword when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error.

Enumerated Attribute Values

Syntax

<!ATTLIST element-name attribute-name (en1|en2|..) default-value>

Example

DTD:
<!ATTLIST payment type (check|cash) "cash"> XML example:
<payment type="check" />
or
<payment type="cash" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE payment [
<!ELEMENT payment EMPTY>
<!ATTLIST payment type (check|cash) "cash">
]>
<payment/>

Use enumerated attribute values when you want the attribute value to be one of a fixed set of legal values.

DTD - Attributes的更多相关文章

  1. DTD总结

    DTD 可以检测 XNM 文档的结构是否正确,就好像文章中用来保证结构正确的语法规则一样. 引入 DTD 1.引入私有的 DTD 文件,URI 可以使相对地址或绝对地址 <!DOCTYPE 根元 ...

  2. How to read the HTML DTD

    Contents How to read the HTML DTD 1. DTD Comments 2. Parameter Entity definitions 3. Element declara ...

  3. XML约束——DTD约束

    参考: 方立勋老师的讲课视频.   什么是XML约束 •在XML技术里,可以编写一个文档来约束一个XML文档的书写规范,这称之为XML约束. 为什么需要XML约束 常用的约束技术 •XML DTD • ...

  4. Expecting "jsp:param" standard action with "name" and "value" attributes错误

    错误信息如下: Servlet.service() for servlet [jsp] in context with path [/20161017] threw exception [/tag/s ...

  5. DTD - XML Building Blocks

    The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Doc ...

  6. Introduction to DTD

    A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines th ...

  7. DTD Tutorial

    The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML doc ...

  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    关于网页中第一行<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  9. XML概念定义以及如何定义xml文件编写约束条件java解析xml DTD XML Schema JAXP java xml解析 dom4j 解析 xpath dom sax

    本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件 XML来 ...

随机推荐

  1. http://blog.csdn.net/xyang81/article/details/7292380

    http://blog.csdn.net/xyang81/article/details/7292380

  2. Java泛型:类型擦除

    类型擦除 代码片段一 Class c1 = new ArrayList<Integer>().getClass(); Class c2 = new ArrayList<String& ...

  3. Android:使用ViewPager实现左右滑动切换图片(图上有点点)

    在以下实例的基础上加上点点 Android:使用ViewPager实现左右滑动切换图片 (简单版) 效果预览: 因为要把点点放图片上,所以修改布局为相对布局: <?xml version=&qu ...

  4. win8 hyper-v 禁用不必卸载虚拟机

    转载:http://tylzwp.blogbus.com/logs/232938121.html 禁用hyperv的目的是使用之前在用的VMware的虚拟机,不必重新处理一遍. 具体操作: 1确报之前 ...

  5. Flash Builder 4.6 基本设置

    Flash Builder 4.6 字体修改 打开Flash Builder4.6 -> 窗口 -> 首选项 -> 左边目录选择常规 -> 外观 -> 颜色和字体 -&g ...

  6. poj 1260 Pearls(dp)

    题目:http://poj.org/problem?id=1260 题意:给出几类珍珠,以及它们的单价,要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 珍珠的替代必须是连续的,不能跳跃 ...

  7. Jquery动画第二部分

    效果图:     →→→→→→ <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  8. 使用Amoeba 实现MySQL DB 读写分离

    Amoeba(变形虫)项目是一个开源框架,于2008年开始发布一款 Amoeba for Mysql软件: 这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当SQ ...

  9. 图片处理 Pillow

    Pillow 在python3下用PIL做图像处理 Python图像处理库:Pillow 初级教程 from PIL import Image im = Image.open('22.gif') pr ...

  10. [OFBiz]简介 二

    1. 执行ant run-install后,生成了55个ofbiz的jar.加上最初的E:\apache-ofbiz-10.04\framework\entity\lib\ofbiz-minerva. ...