News for hackers. Stuff that matters.
Re:XPath is actually pretty useful once it stops being confu... by sixbrx on Monday November 04, 2013 @11:35AM (#6669491)
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 :)
Re:XPath is actually pretty useful once it stops being confu... by fein on Monday November 04, 2013 @11:38AM (#6669516)
It's the latter (XML namespaces spoil everything).

In every single task I do that involves munching on XML with xpath, the largest timesink is figuring out how the ns needs to be set up. (Looking you you, PHP simpleXML).

Re:XPath is actually pretty useful once it stops being confu... by masklinn on Monday November 04, 2013 @11:43AM (#6669550)
> 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 (although that's understandable considering CSS selectors predate XPath), most of the improvements since CSS1 were in XPath day 1, and the rest (pseudo-classes) could probably have been implemented using functions.

Also, that might have finally gotten us a non-eye-stabbing standard function for "match any item of a space-separated list in an attribute" (matching HTML classes in XPath without custom helpers is the worst)

Re:XPath is actually pretty useful once it stops being confu... by yeahbutbut on Monday November 04, 2013 @01:32PM (#6670235)
> Also, that might have finally gotten us a non-eye-stabbing standard function for "match any item of a space-separated list in an attribute"

CSS handles this rather nicely:

    [class~=foo]
https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_s...
Re:XPath is actually pretty useful once it stops being confu... by masklinn on Monday November 04, 2013 @03:22PM (#6670916)
Whereas xpath... does not. Which is a severe understatement considering the equivalent to the CSS selector you wrote up (or to `.foo`) in xpath 1 is something along the lines of:

    [contains(concat(' ', normalize-space(@class), ' '), ' foo ')]
the normalize-space can be dropped IIF you're certain all spaces are normalized, the spaces around the needle not.

xpath 2 does quite a bit better through `tokenize`:

    //*[tokenize(@class, '\s+')='foo']
but still not great. And god forbid you need to match multiple classes in the same selector.

[0] or xpath 1 + exslt if your xpath implementation provides it. exslt actually does slightly better as the pattern is optional and defaults to whitespace characters

Re:XPath is actually pretty useful once it stops being confu... by voltagex_ on Monday November 04, 2013 @07:41PM (#6672293)
Over in .NET land we appear to be stuck on XPath 1.0 forever. A project I used to work on used it extensively, but I now use the HtmlAgilityPack (badly formatted HTML) or XDocument (XHTML strict or XML) where I have the choice.
Re:XPath is actually pretty useful once it stops being confu... by masklinn on Tuesday November 05, 2013 @03:43AM (#6673965)
> Over in .NET land we appear to be stuck on XPath 1.0 forever.

I don't think it's a bad idea, most of the improvements in XPath 2 are the new standard functions which depending on your XPath implementation may be available as extensions (e.g. tokenize comes from exslt, an xpath 1.0 library) but along with that it brings significantly higher complexity and I think the spec has gone from "difficult to read" to "meaningless word-salad".

I really like XPath, but I can't say I was impressed by XPath 2, it loses much of xpath's simplicity with little to show for the added complexity.

Re:XPath is actually pretty useful once it stops being confu... by arethuza on Monday November 04, 2013 @11:52AM (#6669606)
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

Re:XPath is actually pretty useful once it stops being confu... by mcburton on Monday November 04, 2013 @12:50PM (#6669993)
The SAXON XSLT processor implements basic XPATH 2.0

http://saxon.sourceforge.net/

Re:XPath is actually pretty useful once it stops being confu... by yogo on Monday November 04, 2013 @01:18PM (#6670152)
Yep very true. Depending on what you are working on alternative approach is to strip out the namespaces before querying anything. Obviously, this can get you in trouble, and while namespaces are there for a reason, they can be a PITA when working with XML.
Re:XPath is actually pretty useful once it stops being confu... by lobster_johnson on Monday November 04, 2013 @01:11PM (#6670123)
I wonder why libraries make it harder to combine XPath and namespaces. For example, in Nokogiri (libxml2 bindings for Ruby, and probably the de facto XML library in Ruby), you have to do:

    doc = Nokogiri::XML(stream)
    doc.xpath("//x:foo", {x: "http://somenamespace/"}).each ...
I always end up declaring the namespaces in a constant and passing it in, because there is no way to specify your mappings globally. It should have been something like:

    doc = Nokogiri::XML(stream)
    doc.namespaces = {x: "http://somenamespace/"}
    doc.xpath("//x:foo").each ...
or at least something functional like:

    doc = Nokogiri::XML(stream)
    doc.using_namespaces(x: "http://somenamespace/").
      xpath("//x:foo").each ...
I have seen this way of integrating namespaces and XPath in several libraries, it's not just Nokogiri.
Re:XPath is actually pretty useful once it stops being confu... by dionidium on Monday November 04, 2013 @12:40PM (#6669932)
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.