haskellxpathhxt

Why doesn't HXT's xpath searcher return results for a simple query?


In http://www.xpathtester.com/xpath i tested that //i in should work. Why doesn't it?

import Text.XML.HXT.Core
import Text.XML.HXT.XPath
import System.Environment
runX $ readString [] "<r><i/></r>" >>> getXPathTrees "/" >>> writeDocumentToString []
-- ["<r><i/></r>"]
runX $ readString [] "<r><i/></r>" >>> getXPathTrees "//i" >>> writeDocumentToString [] 
-- [""]

Solution

  • The evaluation of the XPath expression is wrong. For example, these queries

    runX $ readString [] "<r><i/></r>" >>> getXPathTrees "/*[1]" >>> writeDocumentToString [] 
    runX $ readString [] "<r><i/></r>" >>> getXPathTrees "/r" >>> writeDocumentToString []
    

    should return the "r" element (the root element of the XML document), but return

    ["<i/>"]
    

    It seems that getXPathTrees returns the content of the matched expression, so you have to return the parent of the node:

    runX $ readString [] "<r><i/></r>" >>> getXPathTrees "/r/i/.." >>> writeDocumentToString []
    ["<i/>"]
    
    runX $ readString [] "<r><i id='1'/><i id='2'/></r>" >>> getXPathTrees "//i/.." >>> writeDocumentToString []
    ["<i id=\"1\"/><i id=\"2\"/>"]
    

    (I don't use HXT, and this is so wrong that I wonder if there may be another clean way to get the node without jumping to the parent…)