Menu

#127 issue when ed -d (delete) is specified multiple times

all
open
nobody
None
5
2022-12-07
2022-12-07
No
$ cat test.xml 
<a>
    <b val="10" />
    <b val="20" />
    <b val="30" />
</a>

$ xmlstarlet ed -P -S -d '/a/b[1]' -d '/a/b[2]' test.xml | xmlstarlet format -s 4 -
<?xml version="1.0"?>
<a>
    <b val="20"/>
</a>

Shouldn't the result be the following

<?xml version="1.0"?>
<a>
    <b val="30"/>
</a>

Version information

$ xmlstarlet --version
1.6.1
compiled against libxml2 2.9.4, linked with 20910
compiled against libxslt 1.1.33, linked with 10134

Discussion

  • Dagobert Michelsen

    Hi Sridhar,

    the modifies are processed in order and are especially not applied all-at-once to the originating tree. This means you first delete the first element (10) leaving two elements (20, 30), than you delete the second element from this result (30), so the remained is 20. If you want to delete the first two elements you can do so by

    xmlstarlet ed -P -S -d '/a/b[1]' -d '/a/b[1]' test.xml | xmlstarlet format -s 4 -
    
     
  • Sridhar Krishnamurthy

    Thank you. it confirms my reasoning too. But the syntactical problem is aggravated if there are just two b elements. In this case the command line looks like xmlstarlet ed -P -S -d '/a/b[1]' -d '/a/b' test.xml | xmlstarlet format -s 4 - which is quite counter intuitive. I respect the designer's decision but would have preferred it otherwise as in the case where the command line is itself generated through scripts.

     

    Last edit: Sridhar Krishnamurthy 2022-12-07

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.