Live data from Hacker News

XPath is actually pretty useful once it stops being confusing

news.rapgenius.com

11–20 of 77 posts

Re: XPath is actually pretty useful once it stops being confusing

#13
post #10

XPath is alright but as sixbrx noted it suffers from problems with namespaces. I keep using this xslt transformation to remove the ns info. Then it works just fine: http://stackoverflow.com/a/413088/34022

This is fine, I guess, and it's clearly something people want to do based on how often it gets asked on Stackoverflow, but there's a reason XML zealots get snarky when people ask how to do this. Some questions you might want to ask yourself:

- Why are the namespaces there in the first place?

- Do I really not care if the element is found in a namespace other than the one expected?

- Does my host environment have a way to specify the namespace of the element I want to find (hint: it probably does)?

- Is the reason that I want to remove the namespace that it's actually something I need to do or is it that I am ignorant of the method for specifying namespaces in my host environment?

Re: XPath is actually pretty useful once it stops being confusing

#15
"This is a perfectly reasonable solution, but it's a whopping 11 lines of code. Further, it feels like we're using the wrong tool for the job: why are we using Ruby iterators and conditionals to get at DOM nodes?"

Is it really that bad to have 11 lines in Ruby?

Initially I didn't get the wrong tool part but after reading it all that did make more sense. I haven't used XPath more than a few times and they were pretty simple so can't complain. Just something I'll have to keep in mind.

Re: XPath is actually pretty useful once it stops being confusing

#16
post #2

I also like XPath for some purposes, but I think it really suffers from (IIRC) having been designed before the xml namespaces, which it only integrates very awkwardly IMO and which ruins the simplicity of XPath. Or maybe XML namespaces spoil everything they affect to some degree :)

My impression when people try to traverse XML while ignoring its namespaces is that they often don't really understand why the namespaces are there in the first place, which indicates to me that they don't really understand the data they're processing, which leads me to believe that any effort they may undertake to extract specific pieces of their document is doomed from the start.

Re: XPath is actually pretty useful once it stops being confusing

#17
post #7
post #4

Earlier quoted context omitted.

> In every single task I do that involves munching on XML with xpath And more generally that's true of every single task involving muching on namespaced XML. Namespaces are a good idea implemented absolutely terribly. XPath is a good idea well-implemented (no, XPath 2 does not exist, there is only one XPath). One of the few I've found in XML-land. I still hate that we have to use CSS selectors rather than XPath (alth…

There has been a spec for XPath 2.0 for quite a while - though I don't think there are many implementations: http://en.wikipedia.org/wiki/XPath_2.0

The SAXON XSLT processor implements basic XPATH 2.0

http://saxon.sourceforge.net/

Post reply on HN