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. linux MySQL安装配置

    执行下面的命令初始化授权表: ./scripts/mysql_install_db --user=mysql

  2. findBugs学习小结

    原文地址:http://www.cnblogs.com/doit8791/archive/2012/10/22/2734730.html 今天代码质量再次强调java代码提交SVN前要经过findBu ...

  3. BZOJ 2553 禁忌

    首先我们要考虑给定一个串,如何将他划分,使得他有最多的禁忌串 我们只需要按里面出现的禁忌串们的出现的右端点排序然后贪心就可以啦 我们建出AC自动机,在AC自动机等价于走到一个包含禁忌串的节点就划分出一 ...

  4. 镜面电火花EDM加工技术资料,模具行业的人应该好好看看!

    目前镜面电火花加工技术在精密型腔模具制造中逐步得以推广.本文就企业中镜面电火花加工应用的关键环节,结合实践分析了影响镜面加工性能的因素.通过控制各个工艺环节,可有效实现高质量.高效率的镜面电火花加工. ...

  5. 意外发现,VC断点可加在构造函数的左括号上

    CTestApp::CTestApp() { // 断点加在这里,然后可单步进入CTestApp的父类CWinApp的构造函数进行调试! ; } 并且在CWinApp的构造函数的左括号上,可进一步进入 ...

  6. DX 绘制位图

    简单地学习了四个API: HRESULT CreateOffscreenPlainSurface( [in] UINT Width, // 宽度 [in] UINT Height, // 高度 [in ...

  7. P147、面试题26:复杂链表的复制

    题目:请实现ComplexListNode* Clone(ComplexListNode* pHead),复制一个复杂链表.在复杂链表中,每个结点除了有一个m_pNext指针指向下一个结点外,还有一个 ...

  8. 解决JDeveloper运行慢的设置/BPM/SOA Server JVM参数设定

    最近在使用 Jdeveloper 10.1.3.3 版本时发现速度奇慢无比,后经Google,发现如下解决方案:在 jdev.conf 文件的末尾加上如下两行,速度即可得到显着的提高, jdev.co ...

  9. urllib

    urllib Python 标准库 urllib2 的使用细节 python 2.x 3.x from urllib import request with request.urlopen('http ...

  10. asp.net微信公众平台开发

    http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97 ...