1

In xmlstarlet how can I select the value of the xmlns attribute?

I tried with

xml sel -t -v //@xmlns input.xml

but didn't get any result. On the other hand

xml sel -t -v //@id input.xml

prints all ids as expected. Do I need to prefix xmlns with some namespace? If yes, which is it?

hooch
  • 1,135
  • 1
  • 16
  • 31
  • Ok, I've found the answer right here: http://stackoverflow.com/questions/122463/how-to-retrieve-namespaces-in-xml-files-using-xpath Sorry for the fuzz! – hooch May 24 '13 at 00:42

1 Answers1

1

In the XPath datamodel, attributes and namespaces are separate node kinds. A namespace declaration in the source XML becomes a namespace node in the data model, not an attribute node, and it's selected using the namespace axis, not the attribute axis. In a namespace node, the name is the namespace prefix, and the value is the namespace URI. If you want to find all the default (unnamed) namespace nodes in the document, you want //namespace::*[name()='']

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thank you, that goes in the right direction. However, what I really want is: Find out whether the default namespace has been set, and if it has been set find out its uri. I'm working with documents which may have all its elements in the empty namespace for convenience. However, in some instances the elements may also reside in a default namespace. So I need a way to treat these 2 cases differently. – hooch May 24 '13 at 00:09