Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book


Today you practiced validating XML documents with DTDs and specified the syntax of XML documents for XML processors to check. In a perfect world, there would be no data-entry errors in XML documents, but real life is a different story. If you specify the syntax of an XML document, you can let an XML processor check that document automatically.

There are two ways to specify the syntax of XML documents—by using DTDs and XML schemas, both of which have syntaxes of their own. You saw today that you use the <!DOCTYPE> element to enclose a DTD and that DTD can either be internal to the XML document (in which case you set the XML declaration's standalone attribute to "yes"), external to the XML document (in which case you provide the DTD's URI and set the XML declaration's standalone attribute to "no"), or a combination of the two. You saw that XML validators can work with either type of DTD.

Today's discussion focuses on writing DTDs and using <!ELEMENT> to declare XML elements. You can use <!ELEMENT>, to use the following syntax for elements: <!ELEMENT name content_model>. Today you saw that there are various content models possible. You can use the content model ANY to allow any content and to turn off syntax checking, or use EMPTY to declare an empty element. You can list possible child elements by using <!ELEMENT document (employee)>, which allows a <document> element to contain an <employee> element.

You can also list the child elements an element can contain, in order, like this: <!ELEMENT employee (name, hiredate, projects)>. Such a list is called a sequence. You can also specify that an element contains text content—parsed character data—by using the term #PCDATA.

You saw today that you can use the symbols + (one or more), * (zero or more), ? (one or none), and | (choices) in DTDs so that you can work with multiple child elements. You can also use these symbols in sequences to specify exactly what child elements or combinations of child elements an element can contain.

Finally, you took a look at working with namespaces in DTDs. Because using a namespace changes the names of elements and attributes, you have to take the new names into account when we're writing a DTD with namespaces. You even have to declare the xmlns attribute that creates the namespace in the first place—and that's a topic we'll hear more about tomorrow.

  • + Share This
  • 🔖 Save To Your Account