Armel Asselin wrote:
I'm searching for a simple command line tool to
manipulate XML files.
I'd recommend building your own tool. This should be
simple enough with saxon8, XQuery and perl/bash/whatever
glue suits you best.
The idea would be commands such as that:
xmanip-tool set /document/xpath[1]/@name="value" remove
//wrong-nodes add /document/node=\<hello\ < in.xml >
out.xml
so, it would simple add/remove/change commands (either
as arguments or input script file), a bit of xpath
The XQuery would be something like this:
-----------------------------------------------------8<---
declare function
local:alter
(
$node as node(),
$alter as node()*
)
{
if ($alter[not(. is $node)])
then
if ($node[self::element()])
then
element
{
name($node)
}
{
for $child in ($node/node()|$node/@*)
return local:alter($child,$alter)
}
else
if ($node[self::document-node()])
then
document
{
for $child in ($node/node()|$node/@*)
return local:alter($child,$alter)
}
else $node
else local:transform($node)
} ;
declare function
local:transform
(
$hit as node()
)
{
( )
} ;
let $a := fn:doc("q1.xml")
let $b := $a//element[@attr]
return local:alter($a,$b)
--->8-----------------------------------------------------
Use the glue to generate XQuery on the fly to use the file
and XPath you need, then feed it to Saxon. Altering
local:transform will let you easily perform various
operations. The above version is 'remove', ( element {
'bork' } { } ) would be 'replace with <bork/>s', ( $hit ,
element { 'bork' } { } ) would be 'insert <bork/>s
after...', etc. etc.
Another idea would be simply learning enough XQuery/XSLT
to code transformations like that without even thinking
about it.
and the usual Unix parameters (-o output,-f file,
handling standard input / output...)
I'm tempted to ask whether unusual, non-Unix parameters
would mean a no go for you. Never mind, I probably don't
want to know.
--
Pavel Lepin