I want to retrieve attribute value, using xpath option (without using xmllint' shell).
my xml:
<?xml version="1.0" encoding="UTF-8"?>
<XML>
<hhi:Elem xmlns:hhi="http://www.w3.org/TR/html4/" version="1.0" sn="101">
</hhi:Elem>
</XML>
this what I tried:
xmllint --xpath '//XML/*[local-name()='Elem']/@sn' FILE_1.xml
XPath set is empty
xmllint --xpath '//XML/*[local-name()='Elem' and namespace-uri()="http://www.w3.org/TR/html4/"]/@sn' FILE_1.xml
XPath set is empty
(I know I can use sed to strip away namespaces but I want to avoid that either)
Best Answer
Yes, don't use
sed
onXML
. It's bad news.I think your problem is actually a simple matter of quoting - you quote your
xpath
in single quotes, andElem
is also in single quotes.I don't have a copy of
xmllint
handy, but this works:That prints:
You don't need the
XML
either, because//
denotes "anywhere in tree". So you could use:For the sake of completeness, with registering the namespace you could do it like this:
Now, I know this doesn't directly help with
xmllint
, aside from allowing testing of thexpath
you're using. But I think it likely that it's quotes tripping you up, not thexpath
.Can I suggest you need: