0

Hi I'm new to xml and DTD.... I just learned this from class 2 hours ago... I have made an XML with a DTD.. I just want to know if my DTD for the XML code is correct?

DTD:

< !DOCTYPE BusinessCard[

    <!ELEMENT BusinessCard (Name?,Phone+,Email?)>
    <!ELEMENT Name (#PCDATA)>
    <!ELEMENT Phone (#PCDATA)>
    <!ATTLIST Phone type CDATA "mobile">
    <!ATTLIST Phone type CDATA "work">
    <!ATTLIST Phone type CDATA "fax">
    <!ELEMENT Email (#PCDATA)>
]>

XML:

< BusinessCard>

    <Name >Jose P. Rizal</Name>

    <Phone type="mobile">(415)555-4567</Phone>

    <Phone type="work">(800)555-9876</Phone>

    <Phone type="fax">(515)555-1234</Phone>

    <Email>joserizal@email.com</Email>

</BusinessCard>
Anshu
  • 7,783
  • 5
  • 31
  • 41
Shan
  • 1
  • 1

1 Answers1

3

Your DTD is syntactically legal, and the document is valid against the DTD, but the triple declaration of the type attribute on the Phone element probably doesn't mean what you want it to mean. The first declaration of a given attribute takes precedence, so what you have is equivalent to

<!ATTLIST Phone type CDATA "mobile">

which means that Phone may take a type attribute whose values can be any character-data (so: any string expressible in XML), and whose default value is "mobile". The two following re-declarations of the same attribute with different default values are ignored.

If you are seeking to say that the attribute can take the values "mobile", "work", or "fax" and no others, what you want to use is an enumerated type:

<!ATTLIST Phone type (mobile | work | fax) "mobile">

If what you want to say is that the attribute can take any value, but the values "mobile", "work", and "fax" are well known values and software should be prepared for them, then you need to say so in prose; there is no way to say just that in DTD notation. You can say something rather similar by giving Phone two attributes (type and othertype), with the rule that type can take the three values in your exercise, and also the value "other", while the othertype attribute takes any string as a value, and has meaning only when type="other". So a home phone number could be tagged <Phone type="other" othertype="home">...</Phone>.

<!ATTLIST Phone type (mobile | work | fax | other) #REQUIRED
                othertype CDATA #IMPLIED >
C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • You say _Your DTD is syntactically legal_, but there is a tool (CLI or web) to check this? Without an XML, just to check if DTD alone syntax; if it is well formed. I couldn't found. – Pablo Bianchi Jun 16 '17 at 20:44
  • Any validating XML processor will report errors in the DTD, so any such processor should work fine as a tool for checking the DTD. Most probably assume (by default) that you are asking them to check the XML document, so if you really want just to check the DTD, you may need to supply a dummy document. – C. M. Sperberg-McQueen Sep 16 '17 at 02:16