[SlugBug] a perl/xslt question. potentially inappropriate.

Chris J cej at nightwolf.org.uk
Sun Apr 17 23:12:55 BST 2005


> That's bloody great, that is. With your help I've managed to write the 
> stylesheet and it seems to work with quite a variety of different 
> pages... all i have to do now is recursively jump about directories, 
> converting xml to wiki and populating the wiki's MySQL database! No 
> problem!
> 
> Something tells me the job's just starting.
> 

I don't know how everything is set up, but here's one possible approach you 
may be able to adapt.

First, create a shell script called, I dunno, "processxml.sh", that takes 
one parameter: the name of the file to insert into database:

#!/bin/sh

# this should chuck out some XML to stdout, so we can redirect
/path/to/xmltowikiconversionscript $1 > /tmp/convertedfile

# These next three lines on one. Substitute where appropriate.
echo "INSERT INTO whatevertable (x, y) VALUES ('$1', '`cat /tmp/convertedfile`'
)" | /path/to/mysql/bin/mysql -u <user> --password=<pass> -h <server> -D 
<database>

## End of script.

Then cd to the top level of the directory where all your XML files are, 
then:

	find . -name '*.xml' -print -exec /path/to/processxml.sh {} \;

And that's it ... job done :-) Obviously the bit that you need to test & 
tweak is the automatic SQL creation in the shell script, all it's doing is 
piping an INSERT into mysql, which should work. You'll need to do whatever 
pre-processing is nesseceary in /tmp/convertedfile to ensure that it's SQL 
compatiable for an INSERT statement (e.g., single quotes are suitably 
escaped so they don't cause a syntax error).

This I'll leave as an exercise for the reader :-)

Of course, if you've done the XSL transform in a perl script with XML::XSLT,
you can do all your database inserts in the same perl script using the DBI 
module to talk to mysql. Get that perl working for one XML file, then you 
can use 'find' as above to run through all your XML :-)

It's not the most efficient method (after all, it's going to open and close 
a connection to MySQL for every XML file you're processing), but it gets 
the job done :-)

Chris...

-- 
\ Chris Johnson                 \ NP: Stevie Nicks - 05. Stand Back
 \ cej at nightwolf.org.uk          \  
  \ http://cej.nightwolf.org.uk/  \ 
   \ http://redclaw.org.uk/        ~---------------------------------------




More information about the SlugBug mailing list