-4

I am really struggling with writing the dtd for some xml code. Despite reading up on this I am stuck as to where I am going wrong. Any advice would be gratefully received. I know it is basic but I am new to this and it is not clicking.

Here is the dtd (amended)...

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Refs [
<!ELEMENT Refs (Book)>
<!ELEMENT Book (author,editor,pdate,title,pplace,pname)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT editor (#PCDATA)>
<!ELEMENT pdate (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT pplace (#PCDATA)>
<!ELEMENT pname (#PCDATA)>
]>

Here is the xml file...

<Refs>
<Book>
    <author>RANGANATHAN</author>
    <editor>anon</editor>
    <pdate>1967</pdate>
    <title>PROLEGOMENATOLIBRARYCLASSIFICATION</title>
    <pplace>NEWYORKNY</pplace>
    <pname>ASIAPUBLISHINGHOUSE</pname>
</Book>
 </Refs>

2 Answers2

0

From what you posted, your document must have Refs as document (top-level) element, but your example XML has Book as document element. So you could change your document and enclose everything in a Refs top-level element.

If the XML PI gives trouble, you can just leave it out if you're only dealing with XML markup anyway; it's not required for XML, and only useful if you intend to use other markup such as HTML and/or SGML in your app.

The ]> character sequence at the end of your prolog must not have a space between, so remove the newline character.

What you're showing as DTD is actually a document prolog and must be the begin of the XML file itself; if you want it in a separate .dtd file, leave out the DOCTYPE declaration and remove the closing ]>, so that the external .dtd file just contains the element declarations. You can then reference the external .dtd file by using <!DOCTYPE Refs SYSTEM "refs.dtd"> as the first line in your XML file (where refs.dtd contains just the element declarations).

The XML PI also (to the best of my knowledge) isn't expected as part of an external DTD, but rather just used at the begin of a document prolog.

Also, what's up with the garbage characters at the begin of your DTD?

imhotap
  • 2,275
  • 1
  • 8
  • 16
  • 1
    "_If the XML PI gives trouble, you can just leave it out if you're only dealing with XML markup anyway; it's not required for XML_" - The XML declaration isn't required for XML version 1.0, but it is [required for version 1.1](https://www.w3.org/TR/xml11/#sec-prolog-dtd). – Daniel Haley Sep 15 '17 at 16:28
0

I'm not sure what validator you're using so I'm not going to comment on where the prolog is located. (See External referenced DTD in XML for an explanation on using an external DTD. This might not be required though depending on what you're using to validate your XML.)

To fix your current setup, try changing DOCTYPE Refs to DOCTYPE Book (the doctype must match the root element). Alternatively, you could add the Refs root element like mentioned in the other answer.

The other answer also mentioned that you need to remove the newline between ] and > in the closing of your doctype declaration. Although having ]> together on the same line is common, it is not required.

You also need to remove the  which is what a BOM (byte order mark) looks like when it's not encoded correctly. You can't have any characters before the <?xml declaration. An XML declaration in an external DTD is fine.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • Thank you both very much. I am going to amend it in line with your advice and will post it to see if I have done it correctly if that's OK. – Android Me Sep 15 '17 at 16:33
  • OK, have amended original. If you would be kind enough to take a look, I would appreciate it. Thanks. – Android Me Sep 15 '17 at 16:44
  • Amended in original post. – Android Me Sep 15 '17 at 16:48
  • @DanielNot one this now, thanks Daniel. Am now on to the longer exercise (of which this was a small part). Having problems at the end. Will try and figure it out and post if no joy. Thanks for your help and getting back to me. It is very much appreciated. – Android Me Sep 20 '17 at 08:04