xmlxml-parsingdtd-parsingdtd

Knowing if the corresponding XML is correct with the Given DTD script


How would I know if The XML that I created follows the rule that is in my DTD?

here's my XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE HW SYSTEM "HW.dtd">
<A>
    AA = "BAR"
    AB = "FOO"
    AC = "1"
    <C>
        <B/>
        CA = "Name"
    </C>
    <D>Element</D>
</A>

and here is my DTD

<?xml version="1.0" encoding="ISO-8859-1"?>
<!ELEMENT A (B* |C+,D)>
<ATTLIST A
    AA CDATA #REQUIRED
    AB CDATA #REQUIRED
    AC(0|1|2) #REQUIRED
>
<!ELEMENT B EMPTY>
<!ATTLIST C(B)>
<!ATTLIST C
    CA CDATA #REQUIRED
>
<!ELEMENT D (#PCDATA)>

is my XML correct with the DTD?


Solution

  • You need to Use an XML Validator,

    An Online version is http://www.xmlvalidation.com/, there are lots of tools that do this also

    According to the validator, your DTD is not valid

    I've tried to fix up your DTD, here's the fixed up version

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!ELEMENT A (B* | (C+,D))>
    <!ATTLIST A
        AA CDATA #REQUIRED
        AB CDATA #REQUIRED
        AC (0|1|2) #REQUIRED
    >
    <!ELEMENT B EMPTY>
    <!ELEMENT C EMPTY>
    <!ATTLIST C
        CA CDATA #REQUIRED
    >
    <!ELEMENT D (#PCDATA)>
    

    The problems with the DTD you posted were:

    As far as I could see you need the brackets around C+,D also the <ATTLIST above is incorrect as you specify a list of attributes with <!ATTLIST note the ! was missing.

    Also your DTD had

    <!ELEMENT B EMPTY>
    <!ATTLIST C
        CA CDATA #REQUIRED
    >
    

    Which failed validation as you were defining an attribute list from element C <!ATTLIST C but hadn't defined the Element C in the DTD, therefore I changed it to

    <!ELEMENT C EMPTY>
    <!ATTLIST C
        CA CDATA #REQUIRED
    >
    

    This makes your DTD valid

    Also your XML is wrong in quite a few ways

    for example your DTD specifices

    <!ATTLIST A
        AA CDATA #REQUIRED
        AB CDATA #REQUIRED
        AC (0|1|2) #REQUIRED
    >
    

    Which means that Element A must have the attributes AA BB AC as they are required also AC attribute must be 0 or 1 or 2

    You have put

    <A>
        AA = "BAR"
        AB = "FOO"
        AC = "1"
    

    Which is not specifiying XML attributes in the Element A, to specify attributes it should be

    <A AA="BAR" AB="FOO" AC="1">
    

    This should help you do your homework

    heres a link http://www.quackit.com/xml/tutorial/dtd_introduction.cfm to a DTD tutorial this should help you create an XML doc that validates against it