Well-Formed XML Documents If an XML document is not understood successfully by an XML processor then the processor cannot format the document. To handle that, XML documents are subject to two constraints: well formedness and validity, well formedness being the basic constraint. Well-Formed Document As set by the W3C, for an XML document to be well formed it should follow the document production containing three parts in the document.
The prolog should include an XML declaration such as <?xml version="1.0"?>. It can also contain a Document Type Definition (DTD). The root element of a document can hold other elements and the document should contain exactly one root element. All other elements should be enclosed within the root element. The optional miscellaneous part can be made up of XML comments, processing instructions and whitespaces. Also the XML document should follow the syntax rules specified in the XML 1.0 recommendation set by W3C. An example of a well formed document is listed below :
Understanding the above document for well-formedness: The document starts with a prolog, which is the xml declaration. The First element, which is the root element is the <DOCUMENT> element which contains all other elements. Next is the <CONSUMER> element inside the root element which is for two consumers. For each consumer, their name is stored in the <NAME> element which itself contains elements like <FIRST_NAME> and <LAST_NAME>. The details of the purchases which the consumer made is stored in the <ORDER> element in the <PURCHASE> element which in turn contains the elements <ITEM><QUANTITY><PRICE> which records the item purchased, quantity and price which the consumer purchased. The document ends with the closing </DOCUMENT> element. Data can be stored for as many consumers as wanted and handling such kind of data is not a problem for the XML processor. The following are the basic rules that should be kept on mind when creating a Well-Formed XML document.
Documents like the one above can be extended as long as we can. XML doesn't have any problem handling such kind of documents, as long as they are wellformed. | ||
Valid XML Documents An XML document is said to be valid if it has a Document Type Definition (DTD) or XML schema associated with it and if the document complies with it. DTD's are all about specifying the structure of the document and not the content of the document. And with a common DTD many XML applications can be shared. Such is the importance of a DTD. Let's take a look at the example which was created in the section Well-Formed XML documents.
Adding a DTD to the example above makes the code look like this:
Breaking the DTD for understanding: Note the first line of the DTD, <!DOCTYPE DOCUMENT[. That line is the document type declaration.<!DOCTYPE> is the syntax to declare a DTD and it should be followed by the root element, which in this example is the DOCUMENT element. Each element should be specified with the syntax <!ELEMENT>. Using that declaration we can specify whether each element is a parsed character data (#PCDATA, used for storing plain text) or can contain other elements in it. In the example above the CONSUMER element is written like this <!ELEMENT DOCUMENT(CONSUMER)*>.The asterik(*) here indicates that the CONSUMER element can have zero or more occurrences. In the example above, it has two occurrences. The next element in the CONSUMER element is the NAME element which in turn contains the elements FIRST_NAME and LAST_NAME within it. Both the FIRST_NAME and LAST_NAME elements are declared as #PCDATA which allows them to handle plain text. The next element in the DTD is the PURCHASE element with an asterik(*) which means that it has zero or more occurrences. The elements within the PURCHASE element is the ORDER element which in turn include the elements ITEM, QUANTITY and PRICE. The elements ITEM, QUANTITY and PRICE are declared as #PCDATA as they hold only plain text. That's how a basic DTD looks like. A DTD like the one above is said to be an internal DTD. We can also create external DTD's and it's these external DTD's which allows us to share a common XML document within different organizations. For more information about how to insert attributes, comments, etc in DTD's please refer to the W3C specification for XML DTD's. The image below shows how the above code when opened in an browser looks like. |
WIDE COLLECTION OF DOTNET TUTORIALS AND MCA STUDENTS JOB OPENINGS/INTERVIEW QUESTONS ......MUCH MORE
Tuesday, July 19, 2011
XML SAMPLE CODE
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment