falstaff
falstaff

Reputation: 3693

Select value with Linux command xpath (from Perl)

I want to get an attribute out of a XML-Tree. Therefor I use the command xpath from package libxml-xpath-perl. I figured out this command:

virsh dumpxml save | xpath -e "/domain/devices/disk[@type='file']/source/@file[1]"

This gives me

 file="/var/lib/libvirt/images/save.raw"

How can I select the value only? (/var/lib/libvirt/images/save.raw)

Thanks

falstaff

Upvotes: 0

Views: 2746

Answers (4)

andre-r
andre-r

Reputation: 2705

I think returning only the value is normal behaviour. Maybe it is a bug? I tried /text() like Tomalak suggested and it returns nothing (at least with this tool).
You could now pipe the output in another command like sed to get the desired result:

... | sed -r 's/.*?"(.*)"/\1/'

Edit:
Apparently the perl script uses XML::XPath and uses only find for the query which returns a NodeSet object. But there also is findvalue which the script doesn't use. Maybe you could fiddle around a little with the script. Only replacing find with findvalue gives just the value on stdout, but also error messages on stderr.
Doc for the library: https://metacpan.org/pod/XML::XPath

Upvotes: 0

mirod
mirod

Reputation: 16171

I use (maybe because I wrote it ;--) xml_grep2, from App::xml_grep2, which has a convenient -t option that returns the text value of the result:

virsh dumpxml save | xml_grep2 -t "/domain/devices/disk[@type='file']/source/@file[1]"

should work

Upvotes: 2

Tomalak
Tomalak

Reputation: 338316

/domain/devices/disk[@type='file']/source/@file[1]/text()

Upvotes: 0

Ether
Ether

Reputation: 53986

You want to separate the string based on the =, so use split (perldoc -f split):

my ($attr, $value) = split /=/, 'file="/var/lib/libvirt/images/save.raw"';

However, instead of parsing these values literally, you should use the many libraries of XML parsers, such as XML::XPath.

Upvotes: 0

Related Questions