Problem with xml
I am novice to xml...I just started 开发者_JAVA百科studying xml....I have the following doubts.. The following is my xml code
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE book [
<!ELEMENT book (page)>
<!ELEMENT page (heading,#PCDATA)>
]>
<note>
<page>
hhh<heading>c</heading><heading>s</heading>
</page>
</note>
When i opened this in browser ,it shown that there is an error with #PCDATA...when i replaced it with PCDATA it showed no error...According to my DTD, page can contain exactly one heading element...am i right?But when i opened it in browser it showed no error even if i have two heading elements..Why did it happen..Also what is the difference between CDATA and PCDATA....
Use this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note [
<!ELEMENT note (page)>
<!ELEMENT page (#PCDATA|heading)*>
<!ELEMENT heading (#PCDATA)>
]>
<note>
<page>
hhh<heading>c</heading><heading>s</heading>
</page>
</note>
PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser for entities and markup.
CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.
My advice is to pick up some solid validating parser, for example AltovaXML (Community Edition) is very straightforward to use:
altovaxml -validate document.xml
Let's look what's wrong with your DTD. First of all your document element (root) is not named book
, so we got first error from here:
Error in referenced Schema or DTD. Element does not match root element name 'book' from the DTD.
Second thing is that heading
is not declared:
Element has not been declared.
Finally to allow mixed content put choice with #PCDATA
(that means parsed character data) at first and heading
element:
Finally your DTD is:
<!DOCTYPE note [
<!ELEMENT note (page)>
<!ELEMENT page (#PCDATA | heading)*>
<!ELEMENT heading (#PCDATA)>
]>
精彩评论