Menu

#5 manipulate strings via shell commands

open
nobody
None
7
2011-04-09
2003-10-22
No

Hi

Let's say I have an SVG file and want to divide all
width attributes of all rect elements by two.

If the value of the selected attribute would be
automatically piped to the shell command then something
like the following could work:

xmlstar ed -u "//rect/@width" -r "ruby -e 'print
$stdin.read.to_f/2'"

"-r" for result, or "-s" for shell perhaps.

Tobi

Discussion

  • Mikhail Grushinskiy

    Logged In: YES
    user_id=643949

    Running another process for this is too much overhead.
    It could have been done just with XPATH.

    Currently 'ed' option doesn't allow expressions based on
    current values in the document. But this is planned (see xml
    ed -x option).

    Here is how I think it will be possible in future

    xml ed -u "//rect/@width" -x "@width/2"

    Thanks for pointing this. I know that this is serious deficiency,
    as there can bee need to modify somesthing in the document
    based on what is already there (ex increment value).

    --Mikhail

     
  • Anonymous

    Anonymous - 2003-10-22

    Logged In: YES
    user_id=769974

    Hi Mikhail

    > Running another process for this is too much overhead.

    Not in all scenarios. XMLStarlet is quite fast and
    lightweight (unlike
    xsh or Java tools for example).

    On Unix it is very common to call two, three, and more tools
    in one
    command line (chained pipes). Calling XMLStarlet with a
    command that
    involves calling tools like sed or tr still introduces very
    little
    overhead.

    > It could have been done just with XPATH.

    Yes, it's just a simple example meant as illustration.

    > Currently 'ed' option doesn't allow expressions based on
    > current values in the document. But this is planned (see xml
    > ed -x option).

    I know :)

    > Here is how I think it will be possible in future
    >
    > xml ed -u "//rect/@width" -x "@width/2"

    I'm looking forward to this feature, and I'll happily
    feature it in
    http://www.pinkjuice.com/howto/vimxml/ .

    > Thanks for pointing this. I know that this is serious
    > deficiency,
    > as there can bee need to modify somesthing in the document
    > based on what is already there (ex increment value).

    Exactly.

    For cases where XPath is sufficient the above feature (-x) is
    sufficient, lightweight, fast, and useful.

    For cases where XPath is not sufficient/appropriate [1] (eg
    "change
    all element names to be lowercase", "insert current date in
    element
    'last-change'", etc) it would be very useful if we could
    call the
    comandline.

    I really think it would be the best strategy to offer both
    features.
    We need XPath for updated values since it knows XML. But
    XPath is not
    a string manipulation language so we also need to be able to
    call
    tools like sed and tr, and for heavier lifting even full
    languages
    such as Perl, Ruby. Only one simple feature is required
    (pipe the
    string to the shell command), and we have all the power of
    commandline tools available.

    Tobi

    [1]
    complex string manipulation in XPath would involve extremely
    long
    commandlines

     
  • Noam Postavsky

    Noam Postavsky - 2011-04-09
    • priority: 5 --> 7
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.