jk2addict has asked for the wisdom of the Perl Monks concerning the following question:

Good day/night to all. I need to get some advice on how to best deal with reading and writing to possibly unknown XML files.

On my current project, I will be reading and writing 'job request' type information from a queue(s). Sometimes, reading the request is enough, but sometimes they may/will be re-submitted to another queue. At other times, a fresh request will be generated and submitted to a queue for the first time.

In the spirit of flexibility, I'm trying to keep a 'request' and it's destination format as abstract as possible via OOP. A request my live in a text file, it may live in an XML file, a binary file, etc. And it may start life as a text file, and end life as an XML file. That part is pretty straight forward.

Now, down to the XML type of request itself. I can easily verify an XML request is in proper form by validating via a DTD In fact, different requests could have different DTDs. Again, nothing exciting there. Now my question comes down to writing a request. How can I use a DTD to write an XML file given a hash structure of data? I've seen a couple of XML::*Write* modules on CPAN. But nothing clicked in my brain pan in terms of 'That's how to do it'.

Is writing using/through a DTD doc possible, or even reasonable? Would it be better to standardize on one XML doc format, and use XSL/XSLT instead to transform foreign XML requests into and out of the base request DTD format?

I forgot to mention, the queues are also abstracted. A queue may be a directory, a database, a POP3 account, MQueue, etc.

-=Chris

  • Comment on XML, DTD, and Abstraction of a Data File

Replies are listed 'Best First'.
Use XSL
by cebrown (Pilgrim) on Aug 28, 2002 at 22:42 UTC
    Since there isn't a way to create XML from a DTD (it is very unclear why you would want to do this anyway -- DTD defines a standard document while each "job request" is an XML instance of the document), I would suggest that you skip focusing on the incoming DTD's and rally 'round the "use XSL/XSLT instead to transform foreign XML requests into and out of the base request DTD format" idea.

    You could still use each inbound DTD to validate incoming documents to ensure they are, well, valid, before you unleash your XSLT on them.

    You ought to end up with four "phases" in your application thusly -- read from queue, validate document, transform document, write to queue.

      "...it is very unclear why you would want to do this anyway...'

      Just a thought really. Since a DTD or Schema is a roadmap to incoming XML, it would be nice if it could also be reused as a roadmap for outgoing XML (like an i/o filter). Granted, XSL/XSLT is definately a great solution, but on the downside, that means we have to write both a DTD AND an XSL/XSLT stylesheet for the outgoing transformed request. Or make the destination queue owner do it. :-) I was just thinking since we already have a DTD, why not use it again instead of having to create more files to take care and feeding of.

      "You ought to end up with four "phases" in your application thusly -- read from queue, validate document, transform document, write to queue."

      Probably 5 steps for the paranoid at heart; read from queue, validate, transform, revalidate against destination queue DTD, write to queue. :-)

Re: XML, DTD, and Abstraction of a Data File
by grantm (Parson) on Aug 28, 2002 at 22:41 UTC

    Assuming tools were available, I would think XML Schema would be a better fit than DTD for what you're trying to do. Unfortunately there's precious little schema support available for Perl right now. Sorry, I know that's not very helpful :-(