473,414 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

simple command line XML node manipulation tool?

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
Nov 15 '06 #1
6 6783

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

Nov 15 '06 #2
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-optionsare:
-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:oracle-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.
--
To reply by e-mail, please remove the extra dot
in the given address: m.collado -mcollado

Nov 15 '06 #3
>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.
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
Nov 15 '06 #4
>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-optionsare:
-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:oracle-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
Nov 15 '06 #5
Armel Asselin wrote:
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...

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Nov 16 '06 #6
>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

Nov 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
3
by: Aquila Deus | last post by:
Hi all! Is there any command-line tool that can add/delete and replace string and files in .resources?
3
by: | last post by:
I am trying to compile a simple c# class in Web Matrix called howdy.cs: // Program start class public class HowdyPartner { // Main begins program execution public static void Main() { //...
10
by: Mandy | last post by:
I have a command line tool that I would like to run from my .NET web application using System.Diagnostics.ProcessStartInfo. I run cmd.exe with this and then pass the command to run the tool as an...
3
by: Chris Lieb | last post by:
I am new to XML Schema and am running into a bit of a snag. I have defined an XML-based scripting language for an updater program that I am working on. I would like to make a schema for this...
4
by: magix | last post by:
Dear Guru, I'm not so good in file manipulation, but would like to seek for help here. I have following basic code of command line argument as starting point (the code below output the...
3
by: asianmuscle | last post by:
Hi I am relearning datastructure... but got kinda stuck in a basic delete node operation. what it does is to delete the first node it finds when the item is equal the input item. the end result is...
6
by: beantaxi | last post by:
Hello all, I'm looking for a very simple code analysis tool. I have a large codebase to analyze, and all I really need to do is to find all uses of all methods in a few interfaces. Many tools...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.