4 ms·
Xpath would have been nice if you didnt have to pedantically namespace every bit of every query
by codeulike 1y ago
Xpath would have been nice if you didnt have to pedantically namespace every bit of every query
- masklinn 1y agoThat… has nothing to do with xpath? If your document has namespaces, xpath has to reflect that. You can either tank it or explicitly ignore namespaces by foregoing the shorthands and checking `local-name()`.
- codeulike 1y agoOk. 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*
- rhdunn 1y agoNewer versions of XPath and XSLT allow /*:bookstore/*:book/*:title
- ndriscoll 1y agoI 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.
- masklinn 1y ago> 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
- somat 1y agoCan confirm, Working programaticly with XML is not really that bad, there is a well formed query syntax(xpath), the dom api just works. Until some joker decided to employ xml namespaces, then everything turns ugly real fast. I am not sure I can articulate why it is so unpleasant, something about how everything gets super verbose and api now needs all sorts of extra state.