Search | Contact | Link To Us  

      VB .NET    

.NET Defined
OOP with VB
VB Language
Win Forms
Windows Controls
ADO .NET
User Controls
File Handling
Multithreading
Deployment
XML Web Services
Essential XML
Discussions
ASP.NET
Resources 



Advertisement











     Home> XML> Valid XML Documents


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.

<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<CONSUMER>
<NAME>
<FIRST_NAME>
BEN
</FIRST_NAME>
<LAST_NAME>
HOLLIAKE
</LAST_NAME>
</NAME>
<PURCHASE>
<ORDER>
<ITEM>
DVD
</ITEM>
<QUANTITY>
1
</QUANTITY>
<PRICE>
200
</PRICE>
</ORDER>
</PURCHASE>
</CONSUMER>
<CONSUMER>
<NAME>
<FIRST_NAME>
ADAM
</FIRST_NAME>
<LAST_NAME>
ANDERSON
</LAST_NAME>
</NAME>
<PURCHASE>
<ORDER>
<ITEM>
VCR
</ITEM>
<QUANTITY>
1
</QUANTITY>
<PRICE>
150
</PRICE>
</ORDER>
</PURCHASE>
</CONSUMER>
</DOCUMENT>

Adding a DTD to the example above makes the code look like this:   

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DOCUMENT[
<!ELEMENT DOCUMENT (CONSUMER)*>
<!ELEMENT CONSUMER (NAME,PURCHASE)>
<!ELEMENT NAME (FIRST_NAME,LAST_NAME)>
<!ELEMENT FIRST_NAME (#PCDATA)>
<!ELEMENT LAST_NAME (#PCDATA)>
<!ELEMENT PURCHASE (ORDER)*>
<!ELEMENT ORDER (ITEM,QUANTITY,PRICE)>
<!ELEMENT ITEM (#PCDATA)>
<!ELEMENT QUANTITY (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]>
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<CONSUMER>
<NAME>
<FIRST_NAME>
BEN
</FIRST_NAME>
<LAST_NAME>
HOLLIAKE
</LAST_NAME>
</NAME>
<PURCHASE>
<ORDER>
<ITEM>
DVD
</ITEM>
<QUANTITY>
1
</QUANTITY>
<PRICE>
200
</PRICE>
</ORDER>
</PURCHASE>
</CONSUMER>
<CONSUMER>
<NAME>
<FIRST_NAME>
ADAM
</FIRST_NAME>
<LAST_NAME>
ANDERSON
</LAST_NAME>
</NAME>
<PURCHASE>
<ORDER>
<ITEM>
VCR
</ITEM>
<QUANTITY>
1
</QUANTITY>
<PRICE>
150
</PRICE>
</ORDER>
</PURCHASE>
</CONSUMER>
</DOCUMENT>

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.

  Privacy Policy | Terms of Use | Site Map | Contact

  © 2004-2007 Startvbdotnet.com. All rights reserved.