← Back to context

Comment by codeulike

13 hours ago

Ok. Perhaps 'namespace the query' wasnt quite the right way of explaining it. All I'm saying is, whenever I've used xpath, instead of it looking nice like

/*bookstore/*book/*title

its been some godawful mess like

/*[name()='bookstore']/*[name()='book']/*[name()='title']

... I guess because they couldn't bear to have it just match on tags as they are in the file and it had to be tethered to some namespace stuff that most people dont bother with. A lot of XML is ad-hoc without a namespace defined anywhere

Its like

Me: Hello Xpath, heres an XML document, please find all the bookstore/book/title tags

Xpath: *gasps* Sir, I couldn't possibly look for those tags unless you tell me which namespace we are in. Are you some sort of deviant?

Me: oh ffs *googles xpath name() syntax*

> the tags as they are in the file

Is not actually relevant and is not an information the average XML processor even receives. If the file uses a default namespace (xmlns), then the elements are namespaced, and anything processing the XML has to either properly handle namespaces or explicitly ignore namespaces.

> A lot of XML is ad-hoc without a namespace defined anywhere

If the element is not namespaced xpath does not require a prefix, you just write

    //bookstore/book/title

I don't recall ever needing to do that for unnamespaced tags. Are you sure the issue you're having isn't that the tags have a namespace?

my:book is a different thing from your:book and you generally don't want to accidentally match on both. Keeping them separate is the entire point of namespaces. Same as in any programming language.