Search code examples
xmlxmlstarlet

xmlstarlet: how to select element by alphanumeric content


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!


Solution

  • 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