Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML, DTD: how to make the order not important

Tags:

xml

dtd

I started off using an XML file and a parser as a convenient way to store my data

I want to use DTD to check the structure of the xml files when they arrive.

Here is my DTD file

< ?xml version="1.0" encoding="UTF-8"?>
< !ELEMENT document (level*)>
< !ELEMENT level (file,filelName?,fileNumber?)>
< !ELEMENT file (#PCDATA)>
< !ELEMENT filelName (#PCDATA)>
< !ELEMENT fileNumber (#PCDATA)>

(note that fileName and fileNumber are actually purely optional)

and

<document>
 <level>
  <file>group1file01</file>
 </level>
 <level>
  <file>group1file02</file>
  <fileName>file 2</fileName>
  <fileNumber>0</fileNumber>
 </level>
...

as such all this works fine. (I use eclipse "validate" option to test it for now)

however while testing I got what I think is a wierd error

if I do

 <level>
  <levelName>Level 2</levelName>
  <levelNumber>0</levelNumber>
        <file>group1level02</file>
 </level>

changing the order of the lines, Eclipse refuses to validate it ...

I was wondering if this was a problem with Eclipse or if the order is actually important.

If the order is important how can I change the DTD to make it work no matter the ordering of he elements?

I can't really change the XML because I already have all the XML files and the parser written (I know I did it the wrong way round lol).

like image 869
Jason Rogers Avatar asked Jan 20 '11 07:01

Jason Rogers


People also ask

Is the order of XML important?

According to the XML specification, the order of attribute specifications in a start-tag or empty-element tag is not significant.

Are XML attributes ordered?

XML attributes are re-ordered alphabetically by the DOM parser. Attributes are being displayed in alphabetical order suggesting that the parser is sorting them. In Progress 9.1E and OpenEdge 10.0x the sorting of the XML attributes was done by length and alphabet. Progress has upgraded the XML parser within the product.

How is DTD important in XML?

The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference.


3 Answers

As Roger said, there are only ordered lists, but you can use operator OR | to define all accepted combinations

<!ELEMENT level ((file,filelName?,fileNumber?)|(filelName?,fileNumber?,file))>

Look here, there is an example in the section Choices

like image 87
Gaim Avatar answered Sep 20 '22 11:09

Gaim


Declaring unordered lists with occurrence constraints in DTD will often result in long or complicated looking declarations. One big reason for this is that DTDs must be deterministic, therefore even switching to XML Schemas don't necessarily help.

Here is a DTD declaration for element <level> that contains:

  • exactly 1 <file> element
  • 0-1 <fileName> elements
  • 0-1 <fileNumber> elements
  • in any possible order

code:

<!ELEMENT level ( (file, ((fileName, fileNumber?) | (fileNumber, fileName?))?)
                 |(fileName, ((file, fileNumber?) | (fileNumber, file)))
                 |(fileNumber, ((file, fileName?) | (fileName, file))) )>
like image 8
jasso Avatar answered Sep 22 '22 11:09

jasso


You can use ANY keyword if you don't bother too much about validity:

<!ELEMENT level ANY>

I have faced a similar problem here, this two cases may appear:

<Instructors>
  <Lecturer>
  </Lecturer>
  <Professor>
  </Professor>
</Instructors>

<Instructors>
  <Lecturer>
  </Lecturer>
  <Professor>
  </Professor>
</Instructors>

The only solution I found was this:

<!ELEMENT Instructors ANY>

Maybe there are a better solution, but it works fine for my particular problem.

like image 6
rendon Avatar answered Sep 20 '22 11:09

rendon