Monday 15 January 2018

How to specify the reference of a namespace in a DTD file

When an XML document uses a pre-defined DTD file and its element use the reference of a namespace then you can specify the namespace in the DTD file. Now here we create an XML document checkbook.xml and DTD file checkbook.dtd to learn how to specify the reference of a namespace in a DTD file.

Shows the code of the checkbook.xml file.

<?xml version="1.0"?>
<!DOCTYPE checkbook SYSTEM "checkbook.dtd">
<checkbook>
<deposit type="direct-deposit" xmlns="http://namespace.com/checkbook">
<payor>Bob's Bolts</payor>
<amount>987.32</amount>
<date>21-6-2018</date>
<description category="income">Paycheck</description>
</deposit>
</checkbook>


Shows the code of the checkbook.dtd file.

<!ELEMENT checkbook (deposit)*>
<!ELEMENT deposit (payor, amount, date, description?)>
<!ATTLIST deposit type (cash|check|direct-deposit|transfer) #REQUIRED xmlns CDATA #FIXED "http://namespace.com/checkbook">
<!ELEMENT amount (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT payor (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ATTLIST description caategory (cash|entertainment|food|income|work) 'food'>

Here an XML document is created that uses the reference of a DTD file checkbook.dtd, to define the structure of the checkbook.xml file. The code of the checkbook.dtd file. The XML document contains the checkbook element which further contains the deposit element and its child elements. the elements and attributes of the deposit element use the reference of default namespace, http://namespace.com/checkbook.



No comments:

Post a Comment