今天学习了XML的一些基础知识和DTD用法
1.一个完整的xml实例文件:
slideSample.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->
<!-- 用来引用自己的DTD -->
<!DOCTYPE slideshow SYSTEM "slideshow3.dtd" [
<!-- 定义ENTITY,方便更换数据 -->
<!ENTITY product "WonderWidget">
<!ENTITY products "WonderWidgets">
<!-- 定义外部文件的ENTITY-->
<!ENTITY copyright SYSTEM "copyright.xml">
]>
<!-- SUBSTITUTIONS WORK IN ATTRIBUTES, TOO -->
<slideshow
title="&product; Slide Show"
date="Date of publication"
author="Yours Truly"
>
<!-- PROCESSING INSTRUCTION -->
<?my.presentation.Program QUERY="exec, tech, all"?>
<!-- TITLE SLIDE -->
<slide type="all">
<slide-title>Wake up to &products;!</slide-title>
</slide>
<!-- TITLE SLIDE -->
<slide type="all">
<item>©right;</item>
</slide>
<!-- OVERVIEW -->
<slide type="all">
<slide-title>Overview</slide-title>
<item>Why <em>&products;</em> are great</item>
<item/>
<item>Who <em>buys</em> &products;</item>
</slide>
<slide type="exec">
<slide-title>Financial Forecast</slide-title>
<item>Market Size < predicted!</item>
<item>Anticipated Penetration</item>
<item>Expected Revenues</item>
<item>Profit Margin </item>
</slide>
<slide type="tech">
<slide-title>How it Works</slide-title>
<item>First we fozzle the frobmorten</item>
<item>Then we framboze the staten</item>
<item>Finally, we frenzle the fuznaten</item>
<item><![CDATA[Diagram:
frobmorten <------------ fuznaten
| <3> ^
| <1> | <1> = fozzle
V | <2> = framboze
staten--------------------+ <3> = frenzle
<2>
]]></item>
</slide>
</slideshow>
2.这是DTD文件:
slideSample.dtd
<?xml version='1.0' encoding='us-ascii'?>
<!--
DTD for a simple "slide show".
-->
<!-- 定义元素 -->
<!ELEMENT slideshow (slide+)>
<!-- 定义元素属性 -->
<!ATTLIST slideshow
title CDATA #REQUIRED
date CDATA #IMPLIED
author CDATA "unknown"
>
<!ELEMENT slide (image?, slide-title?, item*)>
<!ATTLIST slide
type (tech | exec | all) #IMPLIED
>
<!-- Defines the %inline; declaration -->
<!-- 定义一个外部DTD文件实体 -->
<!ENTITY % xhtml SYSTEM "xhtml.dtd">
%xhtml;
<!ELEMENT slide-title (%inline;)*>
<!ELEMENT item (%inline; | item)* >
<!ELEMENT image EMPTY>
<!ATTLIST image
alt CDATA #IMPLIED
src CDATA #REQUIRED
type CDATA "image/gif"
>
3.创建Document Type Definition(DTD)
元素
? 可选(0或1)
* 0或多
+ 1或多
属性
#REQUIRED 必须要求写
#IMPLIED 可以不写,如果不写程序会自动给它默认值
#FIXED "fixedValue" 必须和fixedValue一样
1.一个完整的xml实例文件:
slideSample.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->
<!-- 用来引用自己的DTD -->
<!DOCTYPE slideshow SYSTEM "slideshow3.dtd" [
<!-- 定义ENTITY,方便更换数据 -->
<!ENTITY product "WonderWidget">
<!ENTITY products "WonderWidgets">
<!-- 定义外部文件的ENTITY-->
<!ENTITY copyright SYSTEM "copyright.xml">
]>
<!-- SUBSTITUTIONS WORK IN ATTRIBUTES, TOO -->
<slideshow
title="&product; Slide Show"
date="Date of publication"
author="Yours Truly"
>
<!-- PROCESSING INSTRUCTION -->
<?my.presentation.Program QUERY="exec, tech, all"?>
<!-- TITLE SLIDE -->
<slide type="all">
<slide-title>Wake up to &products;!</slide-title>
</slide>
<!-- TITLE SLIDE -->
<slide type="all">
<item>©right;</item>
</slide>
<!-- OVERVIEW -->
<slide type="all">
<slide-title>Overview</slide-title>
<item>Why <em>&products;</em> are great</item>
<item/>
<item>Who <em>buys</em> &products;</item>
</slide>
<slide type="exec">
<slide-title>Financial Forecast</slide-title>
<item>Market Size < predicted!</item>
<item>Anticipated Penetration</item>
<item>Expected Revenues</item>
<item>Profit Margin </item>
</slide>
<slide type="tech">
<slide-title>How it Works</slide-title>
<item>First we fozzle the frobmorten</item>
<item>Then we framboze the staten</item>
<item>Finally, we frenzle the fuznaten</item>
<item><![CDATA[Diagram:
frobmorten <------------ fuznaten
| <3> ^
| <1> | <1> = fozzle
V | <2> = framboze
staten--------------------+ <3> = frenzle
<2>
]]></item>
</slide>
</slideshow>
2.这是DTD文件:
slideSample.dtd
<?xml version='1.0' encoding='us-ascii'?>
<!--
DTD for a simple "slide show".
-->
<!-- 定义元素 -->
<!ELEMENT slideshow (slide+)>
<!-- 定义元素属性 -->
<!ATTLIST slideshow
title CDATA #REQUIRED
date CDATA #IMPLIED
author CDATA "unknown"
>
<!ELEMENT slide (image?, slide-title?, item*)>
<!ATTLIST slide
type (tech | exec | all) #IMPLIED
>
<!-- Defines the %inline; declaration -->
<!-- 定义一个外部DTD文件实体 -->
<!ENTITY % xhtml SYSTEM "xhtml.dtd">
%xhtml;
<!ELEMENT slide-title (%inline;)*>
<!ELEMENT item (%inline; | item)* >
<!ELEMENT image EMPTY>
<!ATTLIST image
alt CDATA #IMPLIED
src CDATA #REQUIRED
type CDATA "image/gif"
>
3.创建Document Type Definition(DTD)
元素
? 可选(0或1)
* 0或多
+ 1或多
属性
#REQUIRED 必须要求写
#IMPLIED 可以不写,如果不写程序会自动给它默认值
#FIXED "fixedValue" 必须和fixedValue一样