Reputation: 11
There is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<CurrentUsage xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://cloud.com/api/v1.5/schema/master.xsd">
<Link rel="up" href="cloudurl" type="application/vnd.vmware.vcloud.vm+xml"/>
<Metric name="cpu.usage.average" unit="PERCENT" value="3.4"/>
<Metric name="cpu.usage.maximum" unit="PERCENT" value="3.4"/>
<Metric name="cpu.usagemhz.average" unit="MEGAHERTZ" value="81.0"/>
<Metric name="mem.usage.average" unit="PERCENT" value="15.99"/>
<Metric name="disk.provisioned.latest" unit="KILOBYTE" value="503337805"/>
<Metric name="disk.used.latest" unit="KILOBYTE" value="290391113"/>
</CurrentUsage>
I'm using xmlstarlet tool, and able to get list of values with:
xmlstarlet sel -t -v //@value test.xml
But I really stuck with getting particular value by name of metric (as an example for "cpu.usage.average"). I've read tons of examples, tried to use expressions like:
xmlstarlet sel -t -m "//[@name='cpu.usage.average']" -v //@value test.xml
But this drops me into:
Invalid expression: //[@name='cpu.usage.average'] compilation error: element for-each*
How do I get particular value in such simple case?
Upvotes: 1
Views: 1171
Reputation: 111686
Your XPath is malformed. Use //*[@name='cpu.usage.average']
for -m
as a fix (and also to bypass namespaces -- otherwise you'll have to declare a namespace prefix) and @value
for -v
.
Altogether, this xmlstarlet command
xmlstarlet sel -t -m "//*[@name='cpu.usage.average']" -v @value test.xml
will return
3.4
for your XML, as requested.
Upvotes: 1