DTD validation

K

Khisrav Kamilov

Hey guys,

I would kindly to ask you to do me a favor by answering and explaining
these terms regarding validation of DTD.

the code of dtd looks like that:

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT A (B*,C*)>
<!ELEMENT B (C+,B?)>
<!ATTLIST B K ID #REQUIRED>
<!ELEMENT C (#PCDATA|D|E)*>
<!ELEMENT D (#PCDATA)>
<!ELEMENT E EMPTY>
<!ATTLIST C L IDREF #IMPLIED>

are the below terms valid or not:

1. <A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>
2. <A><B K="A1"><C>abc</C><C>fgh</C></B></A>
3. <A><B K="A1"><C>abc</C></B><C L="A2">def</C></A>
4. <A><C><E/><E></E></C></A>
5. <A><C><E><E/></E></C></A>
6. <A><B K="A1"><C><D>abc</D></C><B K="A2"><C>def</C></B></B></A>
7. <A><B K="A1"><C><D>abc</D></C></B><B K="A2"><C>def</C></B></A>
8. <A><C></C></A>

I have read few times docs about DTD, but some stuff is still not
understandlable for me.
Any answer and explanation would be really appreciated.

Best regards,
Kamilov
 
M

Martin Honnen

Khisrav said:
are the below terms valid or not:

Consider to install and use a validating XML parser or an XML editor
with validation support, then you can check yourself.
 
A

Alain Ketterlin

Khisrav Kamilov said:
the code of dtd looks like that:
[...]

I've taken your fragments and built the following document, which I've
named test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE test [
<!ELEMENT test (A*)>
<!ELEMENT A (B*,C*)>
<!ELEMENT B (C+,B?)>
<!ATTLIST B K ID #REQUIRED>
<!ELEMENT C (#PCDATA|D|E)*>
<!ELEMENT D (#PCDATA)>
<!ELEMENT E EMPTY>
<!ATTLIST C L IDREF #IMPLIED>
]>
<test>
<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>
<A><B K="A2"><C>abc</C><C>fgh</C></B></A>
<A><B K="A3"><C>abc</C></B><C L="A3">def</C></A>
<A><C><E/><E></E></C></A>
<A><C><E><E/></E></C></A>
<A><B K="A5"><C><D>abc</D></C><B K="A6"><C>def</C></B></B></A>
<A><B K="A7"><C><D>abc</D></C></B><B K="A8"><C>def</C></B></A>
<A><C></C></A>
</test>

Note that I've changed the values of IDs and the IDREF to avoid trivial
errors. <test> is something I've introduced only to avoid having to deal
with 8 different documents. The whole DTD is given as an "internal
subset" also to avoid another file (if ot were a real DTD, you would
probably prefer saving it in a "real" file").

Then I've run the document through a document validator. On my machine,
that was:

xlmlint --valid --noout test.xml

xmllint is from the libxml2-utils package on my ubuntu/linux. I'm sure
the equivalent exists with other xml parser/validators.

The error messages have been really informative.

-- Alain.
 
K

Khisrav Kamilov

Hallo Martin and Alain,

First of all thanks for your effort.

@Martin: I know,it is easiest way to check via validator, but i need
these knowledges for exam, where I have to write all by hand.

@Alain: Thanks for your try.

I didnt probably write clearly my problem, as i know the right answers
of these conditions and I wanted you guys help me with explanation.
For instance: Term 1. <A><B K="A1">abc</B><C>cde<D>fgh</D></C></A> is
unvalid, but i dont see really an error that it is unvalid.
I would be really thanksful if you guys could me explain.

Best regards,
Kamilov
 
M

Martin Honnen

Khisrav Kamilov wrote:
..
For instance: Term 1.<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A> is
unvalid, but i dont see really an error that it is unvalid.
I would be really thanksful if you guys could me explain.


Well taking a validating parser and looking as its error message(s)
should help.
For instance when I take

<!DOCTYPE A [
<!ELEMENT A (B*,C*)>
<!ELEMENT B (C+,B?)>
<!ATTLIST B K ID #REQUIRED>
<!ELEMENT C (#PCDATA|D|E)*>
<!ELEMENT D (#PCDATA)>
<!ELEMENT E EMPTY>
<!ATTLIST C L IDREF #IMPLIED>
]>
<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>

and use jEdits XML plugin to parse it it outputs the following error
message:
The content of element type "B" must match "(C+,B?)".
Your B element looks like this:
<B K="A1">abc</B>
so it has a plain text node with contents "abc" as its sole child, but
no child elements although its definition says it needs to have one or
more C elements followed by zero or one B element. So you need to put at
least one C element in there as in e.g.

<A><B K="A1"><C>abc</C></B><C>cde<D>fgh</D></C></A>
 
K

Khisrav Kamilov

Hi Martin,

thank you so much for your help. now i will look carefully at all DTD
elements.

Best regards

Khisrav Kamilov wrote:

.
For instance: Term 1.<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>  is
unvalid, but i dont see really an error that it is unvalid.
I would be really thanksful if you guys could me explain.

Well taking a validating parser and looking as its error message(s)
should help.
For instance when I take

<!DOCTYPE A [
<!ELEMENT A (B*,C*)>
<!ELEMENT B (C+,B?)>
  <!ATTLIST B K ID #REQUIRED>
<!ELEMENT C (#PCDATA|D|E)*>
<!ELEMENT D (#PCDATA)>
<!ELEMENT E EMPTY>
<!ATTLIST C L IDREF #IMPLIED>
]>
<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>

and use jEdits XML plugin to parse it it outputs the following error
message:
   The content of element type "B" must match "(C+,B?)".
Your B element looks like this:
   <B K="A1">abc</B>
so it has a plain text node with contents "abc" as its sole child, but
no child elements although its definition says it needs to have one or
more C elements followed by zero or one B element. So you need to put at
least one C element in there as in e.g.

<A><B K="A1"><C>abc</C></B><C>cde<D>fgh</D></C></A>
 
P

Peter Flynn

Hallo Martin and Alain,

First of all thanks for your effort.

@Martin: I know,it is easiest way to check via validator, but i need
these knowledges for exam, where I have to write all by hand.

You really, really need to be using a validating editor when you write
these things. That way it's just a click or keypress to get it validated.
@Alain: Thanks for your try.

I didnt probably write clearly my problem, as i know the right answers
of these conditions and I wanted you guys help me with explanation.

It might have been better if you had simply asked for the explanations.
For instance: Term 1.<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A> is
unvalid, but i dont see really an error that it is unvalid.

If we look at that one perhaps this will help:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE A [
<!ELEMENT A (B*,C*)>
<!ELEMENT B (C+,B?)>
<!ATTLIST B K ID #REQUIRED>
<!ELEMENT C (#PCDATA|D|E)*>
<!ELEMENT D (#PCDATA)>
<!ELEMENT E EMPTY>
<!ATTLIST C L IDREF #IMPLIED>
]>
<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>

I get:

onsgmls:test.xml:11:13:E: character data is not allowed here
onsgmls:test.xml:11:13: open elements: A B[1]
onsgmls:test.xml:11:19:E: end tag for "B" which is not finished
onsgmls:test.xml:11:19: open elements: A B[1]

That means abc is not allowed as the content of element type B.
The element is therefore considered as still being unfinished when it
sees the end-tag </B> because the declaration says B must contain at
least one C possibly followed by another B. Text is not permitted.

Change the abc to read <C>abc</C> and you now have a valid document.

///Peter
 
K

Khisrav Kamilov

Hey Peter,

thanks a lot, i have got already one.

Best regards

Hallo Martin and Alain,
First of all thanks for your effort.
@Martin: I know,it is easiest way to check via validator, but i need
these knowledges for exam, where I have to write all by hand.

You really, really need to be using a validating editor when you write
these things. That way it's just a click or keypress to get it validated.
@Alain: Thanks for your try.
I didnt probably write clearly my problem, as i know the right answers
of these conditions and I wanted you guys help me with explanation.

It might have been better if you had simply asked for the explanations.
For instance: Term 1.<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>  is
unvalid, but i dont see really an error that it is unvalid.

If we look at that one perhaps this will help:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE A [
<!ELEMENT A (B*,C*)>
<!ELEMENT B (C+,B?)>
<!ATTLIST B K ID #REQUIRED>
<!ELEMENT C (#PCDATA|D|E)*>
<!ELEMENT D (#PCDATA)>
<!ELEMENT E EMPTY>
<!ATTLIST C L IDREF #IMPLIED>
]>
<A><B K="A1">abc</B><C>cde<D>fgh</D></C></A>

I get:

onsgmls:test.xml:11:13:E: character data is not allowed here
onsgmls:test.xml:11:13: open elements: A B[1]
onsgmls:test.xml:11:19:E: end tag for "B" which is not finished
onsgmls:test.xml:11:19: open elements: A B[1]

That means abc is not allowed as the content of element type B.
The element is therefore considered as still being unfinished when it
sees the end-tag </B> because the declaration says B must contain at
least one C possibly followed by another B. Text is not permitted.

Change the abc to read <C>abc</C> and you now have a valid document.

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top