-1

For an example. inside my xml

<?xml version="1.0"?>
<!DOCTYPE expression SYSTEM "task3-1.dtd">
<expression>
  <left-bracket>(</left-bracket>
  <expression>
    <left-bracket>(</left-bracket>
    <expression>
      <number>24</number>
      <operation>+</operation>
      <number>24</number>
    </expression>
    <right-bracket>)</right-bracket>
    <operation>*</operation>
    <number>5</number>
  </expression>
  <right-bracket>)</right-bracket>
  <operation>-</operation>
  <number>6</number>
</expression>

When i try to run the dtd, it's always error that: The element "expression" has invalid child element 'number'. List of possible elements expected: 'left-bracket'

<!ELEMENT expression (left-bracket+,right-bracket,operation,number+)>
<!ELEMENT left-bracket (#PCDATA)>
<!ELEMENT right-bracket (#PCDATA)>
<!ELEMENT operation (#PCDATA)>
<!ELEMENT number (#PCDATA)>
user3553846
  • 342
  • 1
  • 15

2 Answers2

2

According to your DTD, the <expression> element must contain a sequence of:

  1. One or more <left-bracket> elements, followed by
  2. Exactly one <right-bracket> element, followed by
  3. Exactly one <operation> element, followed by
  4. One or more <number> elements

That's what this line, which represents the content model of <expression> declares:

(left-bracket+,right-bracket,operation,number+)

Since your instance has an <expression> element following <left-bracket> (and not a <right-bracket> or another <left-bracket>), it fails validation.

The error message refers to the <number> element, which also cannot occur in that position inside <expression>. According to the DTD several <number> elements may be present (one is mandatory), but only after all the other elements.

If you are creating an XML file to adhere to the rules of a DTD, you will have to change your document structure. If you are actually designing the DTD to represent some rule you wish to validate in your document, then you have to decide how you wish to represent your data. The way the content model for the expression element was designed won't allow nested expressions, for example.

This DTD validates your instance and allows nested expressions (it might not be exactly what you want, of course - I based it on a quick look at your document structure):

<!ELEMENT expression ( ( (left-bracket,expression+,right-bracket)*,(operation,number)* ) | (number,operation,number) )>
<!ELEMENT left-bracket (#PCDATA)>
<!ELEMENT right-bracket (#PCDATA)>
<!ELEMENT operation (#PCDATA)>
<!ELEMENT number (#PCDATA)>
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • i still have an error at element 'expression' has invalid child element 'expression' – user3553846 Jun 06 '14 at 05:35
  • Yes. Your DTD does not allow nested `` elements. You have to decide which are the rules of your DTD. I suggested something above, but I may be wrong, since I don't know the details of what you are trying to achieve. – helderdarocha Jun 06 '14 at 05:41
  • just get without any error when validate. but yours code still remain the same errors. – user3553846 Jun 06 '14 at 05:49
  • Your source *validates* with the DTD I provided. Perhaps the XML document you are using is not exactly the same as the one you posted. – helderdarocha Jun 07 '14 at 01:45
1

One option is to make everything ? (zero or one)...

<!ELEMENT expression (left-bracket?,expression?,right-bracket?,number?,operation?,number?)>
<!ELEMENT left-bracket (#PCDATA)>
<!ELEMENT right-bracket (#PCDATA)>
<!ELEMENT operation (#PCDATA)>
<!ELEMENT number (#PCDATA)>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95