What's in an element

We have already seen a DTD example, containg elements declarations that, after the declaration, include the element name definition and the element content model:
<!ELEMENT name (first, middle, last)>
If an element contains other elements, they could be organized in sequences or choices.

The name element defined above contains a sequence of elements. The name element should contains all the three specified elements, and they have to be in the specified order.

Say that we want to store contacts in our XML in two alternative formats, like names or references. This constrain is written:
<!ELEMENT contact (name | reference)>
A variation on this schema is a combination of sequences and choices. So, we could write:
<!ELEMENT contact (name | (reference, referee)>
meaning that we could choose if inserting the name or the sequence of two elements, reference and referee.

An element could have a mixed content. We have seen how to say that an element includes text data
<!ELEMENT description (#PCDATA)>
but this won't work if we want give the chance of inserting tags (for instances an HTML em or strong tags) in it. We should explicitly specify which tags is possible to use in that context:
<!ELEMENT description (#PCDATA | em | strong)*>
Here we say that in description free text is expected, and possibly some em and strong tags. Notice the star (*) at the end, meaning that we could have many different sections one after the other.

Sometimes we expect no content for an element, as a typical example we could think to the HTML br tag. If we want the element "marker" being empty, we specify it in the DTD like this:
On the opposite, we could even leave a total freedom on the element content saying that any content is accepted:
<!ELEMENT description ANY>

For more details I suggest you to read Beginning XML by David Hunter et al (Wrox).

No comments:

Post a Comment