tags:

views:

268

answers:

2

Hi all,

Is there any PHP script to convert XML file to DTD.

I tried a lot. I can able to find tools but can't PHP script.

Is there any?

any help will be thankful...

Fero

A: 

Here is something I found that might be of use: http://www.hitsw.com/xml_utilites/

There is also a class that appears to be promising: http://phpclasses.2by2host.com/browse/package/250.html

cdburgess
+3  A: 

Generating a DTD from an XML file is something I've not seen done quite often -- one of the main reasons being that you cannot be sure the XML file you used to generate the DTD will contain everything that the DTD should define.

i.e. the generated DTD will only contain the elements and attributes that were present in that specific XML file -- which doesn't necessarily means all elements and attributes that should be accepted when using it later.


That being said, I haven't found any PHP tool to create a DTD from an XML file...

... But I've found a JAVA one : DTDGenerator - A tool to generate XML DTDs (quoting) :

DTDGenerator is a program that takes an XML document as input and produces a Document Type Definition (DTD) as output.

The aim of the program is to give you a quick start in writing a DTD.
The DTD is one of the many possible DTDs to which the input document conforms.
Typically you will want to examine the DTD and edit it to describe your intended documents more precisely.


A couple of nice things about this DTDGenerator program are :

  • There are some explanations on the website about the basic ideas of how this works
  • The source code is provided
  • It's only less that 600 lines long
    • which means one should be able to understand without too much efforts
    • and possibly port it to PHP ?
    • And there are some comments in the source code -- which would help.
  • It's published under MPL -- which means open source ; which is good, if you want to port it


Starting from that program's source code, it should be possible to write some PHP code (if you cannot execute JAVA, and really need this to be written in PHP) to do the same thing : the basic ideas are not that hard :

  • You have to go through the XML structure
  • building a tree-representation of it, on each starting tag
    • which means elements
    • that can have attributes
  • And, when you've finished going through the XML document, you have to build the DTD from your reprensentation of the document
    • That might be the hard part -- looking at the source of the JAVA tool I linked to, it reprensents quite a large number of lines of code ^^

Nice thing of taking a look at this DTDGenerator before starting is that its author already thought about this process -- and already identified some situations that you might not think about ;-)

Pascal MARTIN