3.2 Elements in the DTD

3.2 Elements in the DTD

The element is the basis for most of the markup in the XML document. In the previous chapter, the exported XML used the field names for the element markup names if you requested FMPDSORESULT. You can use FMPXMLRESULT to produce metadata and generic elements, but it is much easier to see the correlation between the elements as field names and the Document Type Definition if you use FMPDSORESULT in your request.

In Chapter 2 you learned that elements could contain content or other elements or be empty. To define the element, use the keyword <!ELEMENT (case sensitive) followed by the name of the element. If you are using FMPDSORESULT, this is your field name. After the element name, define the content that this element can contain. End the statement with ">".

<!ELEMENT theNameOfTheElement contentSpecification>

The above statement in the DTD is not a processing instruction, such as "<? Do this ?>" or other markup. A declaration for a particular element or attribute is made by starting the statement with the exclamation point (!). The end of the statement does not need to become an empty markup. Do not add the slash (/) at the end of the statement.

The content specification for an ELEMENT can be EMPTY, ANY, show the childrenList, or be of mixedType. EMPTY elements can have attributes but have no content. Elements with the content specification ANY can contain any of the other elements listed in the DTD. No element type may be declared more than once in the definition. Element types are shown below:

EMPTY Element Definition
<!ELEMENT firstname EMPTY> <!-- definition -->
<firstname /> <!-- as it appears in the XML document -->
ANY Element Definition
<!ELEMENT base ANY> <!-- definition -->
<base>
         <!-- all other elements in this document can be used here -->
</base>
Listing 3.4: Element definition with children
Start example
<!-- definition -->
<!ELEMENT customer (firstname, lastname, shipAddr, shipCity, phone)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT shipAddr (#PCDATA)>
<!ELEMENT shipCity (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!-- as these appear in the XML document -->
<customer>
     <firstname>Johann</firstname>
     <lastname>Bach</lastname>
     <shipAddr></shipAddr>
     <shipCity>Leipzig</shipCity>
     <phone></phone>
</customer>
End example

Mixed elements contain content and children. The definition must use the keyword #PCDATA, followed by the pipe character (), which means or, and the list of children. PCDATA means parsed character data and can contain any text content or markup. The definition lists #PCDATA in a mixed data element, followed by the pipe character () and other children. However, the content data can occur before, after, or between children elements, as shown here:

<!ELEMENT customer (#PCDATA | childrenlist)+>
<customer>
     <firstname>Johann</firstname>
     Johann Bach <lastname>Bach</lastname>
</customer>

To further define the children of an element, shortcuts are used in the Document Type Definition. Multiple children are listed in the order they will appear in the document and are separated by commas. Each child can be required, optional, occur zero or more times, or occur one or more times. The question mark (?) is used at the end of the element name or sequence of names to make them optional. This means they may appear but are not required in the document. If they do appear, they are used only once.

The asterisk () is used to specify that an element or sequence of elements appears zero or more times in the document. It is similar to the optional element (?) but may appear multiple times, if at all. To designate an element as required with one or more occurrences allowed, use the plus sign (+) at the end of the element or sequence of elements. This shortcut needs to be included if you have an element with PCDATA and children. You may mix shortcuts along with nested parentheses. We will use people.xml, as shown in Listing 3.5, from Chapter 1 to illustrate mixed elements and shortcuts. Listing 3.6 shows the DTD created for this XML document.

Listing 3.5: people.xml
Start example
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE people SYSTEM "people.dtd">
<people>
     We can use the people element for mixed content.
     <vendor>
          <firstname>John</firstname>
          <company>Paper Cutters</company>
          <phone>555-7894</phone>
     </vendor>
     <customer>
          <firstname>Jane</firstname>
          <lastname>Doe</lastname>
          <phone location="work">555-1234</phone>
          <phone location="home">555-1235</phone>
     </customer>
     Wow! I can intermix the PCDATA between elements.
     <customer>
          <firstname>John</firstname>
          <lastname>Doe</lastname>
     </customer>
</people>
End example
Listing 3.6: people.dtd
Start example
<!DOCTYPE people {
<!ELEMENT people (#PCDATA | vendor* | customer*)>
<!-- the root element, people, can contain content and/or zero or more
  occurrences of vendor or customer -->
<!ELEMENT vendor (firstname?, company, phone)>
<!-- firstname is optional for a vendor, but company and phone are
  required one time -->
<!ELEMENT customer (firstname, lastname, phone+)>
<!-- phone can occur multiple times for customer, but once for vendor -->
<!ELEMENT firstname (#PCDATA)>
<!-- this element needs to be defined only once, even though it is a child
  of vendor and customer -->
<!ELEMENT phone (#PCDATA)>
<!-- we should define the attribute list for phone at this point -->
<!ATTLIST phone
location (work | home | pager)?>
<!ELEMENT company (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
]>
End example


 
ASPTreeView.com
 
Evaluation has ШАИ¶МГЩ·Чexpired.
Info...