I have a piece of XML like so:
<root>
<foo src=""/>
<foo src="bar"/>
<foo />
</root>
I want to know which elements have a src attribute, which are empty and which have values.
The furthest I have come is with
$ xmlstarlet sel -t -m '//foo' -v @src -n foo.xml
bar
Though that doesn't tell me the third foo is missing the attribute.
/root/foo[string-length(@src)!=0]
return all foo elements have non empty value.Unfortunately
/root/foo[string-length(@src)=0]
indicates elements which don't have src attribute and also elements have src attribute but empty.This will select the foos with no
src
attribute.For the other two tasks, I would use a mix of the expressions pointed out by @TOUDIdel and @Dimitre Novatchev:
/root/foo[@src and string-length(@src)=0]
for foos with an emptysrc
, and/root/foo[@src and string-length(@src)!=0]
for foos with ansrc
with content in it.As an aside, I would avoid using the "anywhere" selector,
//
(not to mention the*
wildcard), unless you're sure that this is specifically what you need.//
is like making your very eager dog sniff a piece of cloth and telling it, "bring me everything that smells like this, wherever you find it". You won't believe the weird crap it can decide to bring back.Elements having a
@src
attribute which is empty (no string-value):Elements having a
@src
attribute which has value (string-value):From http://www.w3.org/TR/xpath/#section-String-Functions
From http://www.w3.org/TR/xpath/#function-boolean
Use:
This selects all elements in the XML document that have a
src
attribute whose string-value has length of zero.This selects all elements in the XML document that have a
src
attribute whose string-value has length that is not zero.This selects all elements in the XML document that have a
src
attribute whose string-value after excluding the starting and ending whitespace has length that is not zero.This selects all elements in the XML document that don't have a
src
attribute.