Validation of an xml document using a dtd document

2019-02-26 06:14发布

问题:

I'm new to XML. I'm confused how a DTD document validates an XML document. Below is my code and please correct me if I am wrong.

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE note  
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

<note>
<to>Tove</to>

<rom>Jani</rom> // Here i've miss-spelled the tag <from> but still not getting error

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 
</note>

I know that a DTD is used to validate the XML data. There is an element name <form>. I've just changed the spelling of <form> to <rom> but still did not get any error message in my browser. I'm stuck at this point.

回答1:

Welcome to the XML community.

You will need to find and use a validating parser for your XML; if you want to use a Web browser, you may be disappointed. Older versions of Internet Explorer validate XML against a DTD by default; newer versions don't (but I believe that you can configure them to do so if you can figure out how). Mozilla-based browsers don't have a validating XML parser at all.

Most people who use XML seriously end up using XML-specific tools for validation. Among my favorites for command-line validation are

  • xmllint (the command-line interface to libxml2, which is part of the Gnome project but is also usable outside Gnome)

  • rxp (by Richard Tobin of the University of Edinburgh)

  • Xerces J, Xerces C (these are Apache projects)

There are also XML-aware editors which offer integrated validation from within the editor (sometimes from your choice of several parsers).



标签: xml dtd