1

I am novice to xml...I just started 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....

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90

2 Answers2

4

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.

Community
  • 1
  • 1
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
4

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)>
]>
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • <!ELEMENT page (heading|#PCDATA)> <!ELEMENT heading (#PCDATA)> ]> Why book is not my root element....also ..if <!ELEMENT note(page,heading)> is correct why <!ELEMENT page(#PCDATA,heading)> is incorrect ]> – Jinu Joseph Daniel Aug 28 '11 at 12:37
  • @user822982: Good question, `<!ELEMENT page(#PCDATA,heading)>` is incorrect in DTD, because you can't put `#PCDATA` in **sequence** element content. I think [that thread](http://lists.xml.org/archives/xml-dev/199911/msg00583.html) and its answers should be helpful for you. – Grzegorz Szpetkowski Aug 28 '11 at 14:09