XML Element AttributesArticle Links
Either/OrThe pipe character is used to specify an "OR" operation. Thus, the following DTD snippet would specify an XML document in which
all <!ELEMENT CONTACT (NAME, (PHONE | EMAIL))> <!ELEMENT NAME (#PCDATA)> <!ELEMENT EMAIL (#PCDATA)> Note that XML regular expression matching is not a short circuited system. OR's imply one or the other but not both and not neither. Is that a tongue twister or what!?! Using examples to make my point, here are several invalid XML snippets based on the DTD snippet above.... <CONTACT> <NAME>Jim Sanger</NAME> </CONTACT> That is invalid because the DTD specified that every <CONTACT> <NAME>Jim Sanger</NAME> <EMAIL>Jim Sanger</EMAIL> <PHONE>Jim Sanger</PHONE> </CONTACT> This one is invalid because the contact has BOTH <CONTACT> <EMAIL>Jim Sanger</EMAIL> <NAME>Jim Sanger</NAME> </CONTACT> This one is wrong because NOTE: within a grouping, you may use only one connector (such as , or |). Thus, it is invalid to use <!ELEMENT CONTACT (NAME, PHONE | EMAIL)> Instead, you must create a subgroup as shown above <!ELEMENT CONTACT (NAME, (PHONE | EMAIL))> Optional ChildrenUsing the "?" character specifies that the element named is optional. Thus, in the following code snippet, we specify
that every <!ELEMENT CONTACT (NAME, (PHONE | EMAIL), ADDRESS?)> <!ELEMENT NAME (#PCDATA)> <!ELEMENT PHONE (#PCDATA)> <!ELEMENT EMAIL (#PCDATA)> <!ELEMENT ADDRESS (STREET+, CITY, STATE, ZIP, COUNTRY?) <!ELEMENT STREET (#PCDATA)> <!ELEMENT CITY (#PCDATA)> <!ELEMENT STATE (#PCDATA)> <!ELEMENT ZIP (#PCDATA)> <!ELEMENT COUNTRY (#PCDATA)> Mixed ContentIn certain, probably rare circumstances, you will wish to include parsed character data as a valid element. Mixing content works as expected. Thus, the following XML document would be valid. <?xml version = "1.0" encoding="UTF-8" standalone = "yes"?>
<!DOCTYPE CONTACTS [
<!ELEMENT CONTACTS ANY>
<!ELEMENT CONTACT (NAME | EMAIL | PHONE | #PCDATA)*>
<!ELEMENT NAME (#PCDATA)>
<!ELEMENT EMAIL (#PCDATA)>
<!ELEMENT PHONE (#PCDATA)>
]>
<CONTACTS>
<CONTACT>
<NAME>Roger Kaplan</NAME>
<EMAIL>rabbit@kaplan.com</EMAIL>
<PHONE>1800YOMOMMA</PHONE>
Roger is a swingin' hep cat!
</CONTACT>
</CONTACTS>
Empty ElementsFinally, we must mention the syntax for defining an empty tag. Of course, there is not much to it, you simply use the <!ELEMENT HR EMPTY>
In your XML code, you will have an element such as Defining Valid Element AttributesWell, as you might expect, just as you use the DTD to define valid elements, you also use the DTD to define valid element attributes. We already went over attributes in the last section, but to refresh your memory, we used the following example where
<SHOE STYLE = "SPECTATOR" COLORING = "BLACK_AND_WHITE"> To declare attributes in the DTD you use the general format of: <!ATTLIST ELEMENT_NAME ATTRIBUTE_NAME TYPE DEFAULT_VALUE>
NOTE: Since <!ATTLIST Port
name NMTOKEN #REQUIRED
hostName NMTOKEN #IMPLIED
function %funcType; #REQUIRED
number CDATA #REQUIRED
type %serverType;
serverPort %fbool;
%basicAttrs;>
Of the four pieces, Attribute DefaultsRequiredThe ImpliedWhen you use the FixedSometimes you will want to provide a default value that the document author may not modify. In that case, you will
use Attribute TypesAside from defaults, there are 10 TYPEs of content for attributes including
Let's take a look at each.... CDATA TYPECDATA refers to plain old character data that may be any string of characters that does not include ampersands (&), less than signs, (<), or quotation marks ("). Of course, as we discussed earlier, you may use the escaped characters such as &, <, or " if you must include those forbidden characters <?xml version = "1.0"
encoding="UTF-8"
standalone = "yes"?>
<!DOCTYPE SCRIPT [
<!ELEMENT SCRIPT ANY>
<!ELEMENT DIALOG (#PCDATA)>
<!ATTLIST DIALOG ACTOR CDATA>
]>
<SCRIPT>
<DIALOG ACTOR = "Hanks">I don't think so!</DIALOG>
<DIALOG ACTOR = "Ryan">Why not?</DIALOG>
</SCRIPT>
Enumerated (The keyword is not actually used)A list of acceptable pipe delimited values from which the document author must choose. Note that in the example below, <?xml version = "1.0"
encoding="UTF-8"
standalone = "yes"?>
<!DOCTYPE GROCERY_BASKET [
<!ELEMENT GROCERY_BASKET ANY>
<!ELEMENT MEAT EMPTY>
<!ATTLIST MEAT (CHICKEN |
BEEF |
PORK |
FISH) "CHICKEN">
]>
<GROCERY_BASKET>
<MEAT TYPE = "FISH"/>
<MEAT TYPE = "BEEF"/>
<MEAT/>
</GROCERY_BASKET>
By Selena Sol at eXtropia |
||