增加HTML樣式的文本 既然XML允許你定義任意想要的標(biāo)記,那么你完全可以定義一個類似HTML的標(biāo)記集。實(shí)際上,XHTML標(biāo)準(zhǔn)就做這些工作。你將看到多關(guān)于SAX的說明。目前,下面的粗體文本定義了一個使用HTML樣式的幻燈。 ... <!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title> </slide>
<?xml version='1.0' encoding='utf-8'?> <!-- DTD for a simple "slide show". -->
下一步,增加下面粗體文本到文件: <!-- DTD for a simple "slide show". --> <!ELEMENT slideshow (slide+)> 像你看到的那樣,這個DTD標(biāo)識開始于<!,后面跟隨著標(biāo)是名稱(ELEMENT)。在這個標(biāo)識名稱后識被定義的元素名稱。后面的園括弧里面的內(nèi)容代表著一個或一個以上的slide元素出現(xiàn)在slideshow里都是合法的。
如果沒有和面的加號,那么這個定義代表slideshow只能包含一個slide元素。下面列出了所有的限定符號。 Table 2-2 DTD Element Qualifiers Qualifier Name Meaning ? Question Mark Optional (zero or one) * Asterisk Zero or more + Plus Sign One or more 你能夠包含更多的元素通過使用逗號和在每一個元素上使用限定符。你也能夠使用嵌套來組織多個條目。例如,在定義一個image元素后,你能夠聲明每個image元素必須與一個title元素匹配,通過指定((image, title)+)。在這里,加號是應(yīng)用于image/title對,表明一個或多個指定條目對可能出現(xiàn)。
EMPTY規(guī)范的意思是元素不包含內(nèi)容。所以一個E-MAIL的DTD中的<flag/>可以這樣使用: <!ELEMENT flag EMPTY> 引用DTD 在這個例子中,DTD定義是一個單獨(dú)的文件。也就是說你不得不在XML文檔中引用它。一回你將會看到,在文檔的范圍內(nèi),你也可以包含DTD的一部分。這些定義構(gòu)成了DTD的本地子集。
為了引用DTD文件,你只需要增加下面組體的文本到你的slideSample.xml文件中,并且以slideSample05.xml文件名保存它。 <!-- A SAMPLE set of slides --> <!DOCTYPE slideshow SYSTEM "slideshow.dtd"> <slideshow DTD標(biāo)記開始于“<!”。在這個例子中,這個標(biāo)記名“DOCTYPE”的意思是這個文檔是“slideshow”,“slideshow”得意思是這個文檔由slideshow元素和所有包含在slideshow內(nèi)的內(nèi)容組成。
每一個屬性是由三個空格分開的一系列值構(gòu)成的。逗號和其他的分隔符是不允許出現(xiàn)的,所以像上面那樣格式化定義是有幫助的和易讀的。每一行的第一個元素是屬性名稱:title,date或author。第二個元素表明數(shù)據(jù)類型:CDATA是字符數(shù)據(jù)類型。Table 2-3介紹了所有的合法使用的屬性類型。 Table 2-3 Attribute Types Attribute Type Specifies... (value1 value2 ...) A list of values separated by vertical bars. (Example below) CDATA "Unparsed character data". (For normal people, a text string.) ID A name that no other ID attribute shares. IDREF A reference to an ID defined elsewhere in the document. IDREFS A space-separated list containing one or more ID references. ENTITY The name of an entity defined in the DTD. ENTITIES A space-separated list of entities. NMTOKEN A valid XML name composed of letters, numbers, hyphens, underscores, and colons. NMTOKENS A space-separated list of names. NOTATION The name of a DTD-specified notation, which describes a non-XML data format, such as those used for image files.*
屬性中最后的一個實(shí)體規(guī)范決定屬性的默認(rèn)值。. Table 2-4中顯示了可能的選擇值。 Table 2-4 Attribute-Specification Parameters Specification Specifies... #REQUIRED The attribute value must be specified in the document. #IMPLIED The value need not be specified in the document. If it isn't, the application will have a default value it uses. "defaultValue" The default value to use, if a value is not specified in the document. #FIXED "fixedValue" The value to use. If the document specifies any value at all, it must be the same.
Today's date is ¤tDate;. 你將看到如下的回饋: ... <slide type="all"> <item> This is the standard copyright message that our lawyers make us put everywhere so we don't have to shell out a million bucks every time someone spills hot coffee in their lap... </item> </slide> ...