0

Extracting and dumping elements using xmlstarlet

In this post I've found out, how to select element by its content. Works perfectly!

But no for every value :-(

Here my xml:

<metainfo id="19120454-8234-47EB-B7FE-7691B53788DF" type="volume">
                <id>
                        3138224245
                </id>
                <index>
                        1
                </index>
                <is-last>
                        1
                </is-last>
                <name>
                        asdf
                </name>
                <raw-archive-id>
                        6898476220317415805
                </raw-archive-id>
                <raw-archive-key>
                        977D7B4B-D234-4E95-8BE2-BE0F8E865701
                </raw-archive-key>
                <size>
                        54812566016
                </size>
                <slice-key/>
                <timestamp>
                        1385742689568
                </timestamp>
        </metainfo>

Selecting by id, index or timestamp works perfectly. ("xml" is the command in the windows version ...):

xml sel -t -c "/metabundle/metainfo[timestamp=1385742689568]" test.xml

But something goes wrong if I want to select by another tag, for example "name" or "raw-archive-key":

xml sel -t -c "/metabundle/metainfo[name=asdf]" test.xml

This command will not produce any output. In both tags, name and raw-archive-key, there are alphanumeric characters. If I change "asdf" to "01", it works!

So how to select by content if it contains alphanumeric chars?

Thanks in advance!

Community
  • 1
  • 1
voodalic
  • 3
  • 1
  • Your first working example should read: `xml sel -t -c "/metainfo[timestamp=1385742689568] test.xml` – rhoerbe Nov 28 '17 at 10:02

1 Answers1

2

You need to quote string values, otherwise it looks for a tag with name asdf. Also, you have to use normalize-space() to ignore leading and trailing whitespace:

xml sel -t -c "/metabundle/metainfo[normalize-space(name)='asdf']" test.xml
npostavs
  • 4,877
  • 1
  • 24
  • 43
  • Perfect! I know that string values like to be quoted, but this was not the reason :)>> normalize-space(name) << That did the trick! Thank you! – voodalic Feb 06 '14 at 19:09