simple command line XML node manipulation tool?

A

Armel Asselin

Hello,

I'm searching for a simple command line tool to manipulate XML files.
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 and the usual Unix parameters (-o output,-f
file, handling standard input / output...)

any idea??

thanks in advance,
Armel
 
P

p.lepin

Armel said:
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.
 
M

Manuel Collado

Armel Asselin escribió:
Hello,

I'm searching for a simple command line tool to manipulate XML files.
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 and the usual Unix parameters (-o output,-f
file, handling standard input / output...)

any idea??

XmlStarlet (command "ed"):

e:\mcollado>xml ed --help
XMLStarlet Toolkit: Edit XML document(s)
Usage: xml ed <global-options> {<action>} [ <xml-file-or-uri> ... ]
where
<global-options> - global options for editing
<xml-file-or-uri> - input XML document file name/uri (stdin is used
if missing
)

<global-options> are:
-P (or --pf) - preserve original formatting
-S (or --ps) - preserve non-significant spaces
-O (or --omit-decl) - omit XML declaration (<?xml ...?>)
-N <name>=<value> - predefine namespaces (name without 'xmlns:')
ex: xsql=urn:eek:racle-xsql
Multiple -N options are allowed.
-N options must be last global options.
--help or -h - display help

where <action>
-d or --delete <xpath>
-i or --insert <xpath> -t (--type) elem|text|attr -n <name> -v
(--value) <val
ue>
-a or --append <xpath> -t (--type) elem|text|attr -n <name> -v
(--value) <val
ue>
-s or --subnode <xpath> -t (--type) elem|text|attr -n <name> -v
(--value) <va
lue>
-m or --move <xpath1> <xpath2>
-r or --rename <xpath1> -v <new-name>
-u or --update <xpath> -v (--value) <value>
-x (--expr) <xpath> (-x is not implemented yet)

XMLStarlet is a command line toolkit to query/edit/check/transform
XML documents (for more information see http://xmlstar.sourceforge.net/)
thanks in advance,

Regards.
 
A

Armel Asselin

I'm searching for a simple command line tool to
I'd recommend building your own tool. This should be
simple enough with saxon8, XQuery and perl/bash/whatever
glue suits you best.
I've got a portable C/C++ framework which actually loads/save XML and does a
bit of XPath... i've to see what is a less job (and the easier to setup
later) for me.
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:
[...]
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.
oh, it would not be a 'no go', but if there had been several good enough
tools, I prefer the one which is the 'unix way' rather than the "random
way"...

thank you very much for the tips

Armel
 
A

Armel Asselin

I'm searching for a simple command line tool to manipulate XML files.
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 and the usual Unix parameters (-o
output,-f file, handling standard input / output...)

any idea??

XmlStarlet (command "ed"):

e:\mcollado>xml ed --help
XMLStarlet Toolkit: Edit XML document(s)
Usage: xml ed <global-options> {<action>} [ <xml-file-or-uri> ... ]
where
<global-options> - global options for editing
<xml-file-or-uri> - input XML document file name/uri (stdin is used if
missing
)

<global-options> are:
-P (or --pf) - preserve original formatting
-S (or --ps) - preserve non-significant spaces
-O (or --omit-decl) - omit XML declaration (<?xml ...?>)
-N <name>=<value> - predefine namespaces (name without 'xmlns:')
ex: xsql=urn:eek:racle-xsql
Multiple -N options are allowed.
-N options must be last global options.
--help or -h - display help

where <action>
-d or --delete <xpath>
-i or --insert <xpath> -t (--type) elem|text|attr -n <name> -v
(--value) <val
ue>
-a or --append <xpath> -t (--type) elem|text|attr -n <name> -v
(--value) <val
ue>
-s or --subnode <xpath> -t (--type) elem|text|attr -n <name> -v
(--value) <va
lue>
-m or --move <xpath1> <xpath2>
-r or --rename <xpath1> -v <new-name>
-u or --update <xpath> -v (--value) <value>
-x (--expr) <xpath> (-x is not implemented yet)

XMLStarlet is a command line toolkit to query/edit/check/transform
XML documents (for more information see http://xmlstar.sourceforge.net/)
thanks in advance,

wahoow :) very appealing. i'll check it

thanks
Armel
 
J

Joe Kesselman

Armel said:
any idea??

That sounds simple(minded) enough that you might be able to write
something that would generate stylesheets that did the actual
processing... See my recent comment regarding the "identity transform
with exceptions" approach, which is close to what you're describing.

Or you could just code the stylesheet by hand...
 
A

Armel Asselin

any idea??
That sounds simple(minded) enough that you might be able to write
something that would generate stylesheets that did the actual
processing... See my recent comment regarding the "identity transform with
exceptions" approach, which is close to what you're describing.
I saw the other thread
Or you could just code the stylesheet by hand...
i've done some XSL, but it's explicitly what i do not want to do, at least,
as-is, I mean, one idea is to to make a stylesheet which takes parameters,
but to be usefull it has to accept any number of actions, and of various
form (add/remove/move/replace), and clearly without having to systematically
take the commands from an XML (which is what such actions have to
generate... the egg and the hoose?) and with an XSL stylesheet the dynamic
aspects of parameters together with various commands may not be that easy to
do.
there are many other solutions, the tools proposed by Manuel seem
interesting.

thanks
Armel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top