Search code examples
xmlxpathxmllint

Get name of element using xmllint


Lets say I have this xml:

<Root>
    <Node size="Foo" />
<Root>

In order to get the values of attributes I run this command:

echo "<Root><Node size='foo' /></Root>"  | xmllint -xpath "/Root/Node/@size" -

(note the - at the end)

And this will return foo.

Now my question is how can I have xmllint return me the name of the element which is Node?

I have tried doing:

echo "<Root><Node size='foo' /></Root>"  | xmllint -xpath "/Root/Node/name()" -

but that does not seem to work.


Solution

  • The XPath you're using requires XPath 2.0, while xmllint supports only XPath 1.0.

    Change your XPath to this XPath 1.0 expression,

    name(/Root/Node)
    

    and you'll echo the name of the selected node, Node, as requested.