in reply to XML::XPath

You might look into XML::XSH, which allows you to specify algorithmic parsings and changes of XML using a hybrid of Perl and an "XSH" language. For example, this XSH script would remove your existing filename records, and replace them with the names of the files in the current directory:
open mydoc="thedoc.xml" cd //filename/.. rm filename foreach { glob("*") } { insert element filename into . insert text $__ into filename[last()] } save mydoc
I'm working on getting my head around it enough to write an upcoming magazine article or two.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.