7

Is there a way to interactively search for a nodes that matches a given xpath expression in emacs?

I would like something similar to re-forward-search but instead of using a regular expression I'd type an xpath expression.

5
  • Me too! If I had the time, I'd try to see what nxml code, if any, there is for selecting XPath matches. If such code exists then I would code up an Icicles search multi-command for searching such regions.
    – Drew
    Dec 16, 2014 at 22:40
  • @Drew greping for xpath in nxml code gives no results. There is an xpath.el implementation but I haven't had the time to test it. It requires dom.el to work.
    – manu
    Dec 17, 2014 at 14:36
  • Yep, that's exactly what I found when I last looked for XPath and XQuery support in Emacs. Too bad. Implementing good support for such things would no doubt be non-trivial, but it could be really useful, IMO. (Even if it requires a DOM.) And perhaps it exists (I saw the same reference), but I too do not have the time to really check it out.
    – Drew
    Dec 17, 2014 at 16:38
  • dom.el + xpath.el would do it. What I can do now is to ask for it to the nxml devs. Maybe in the next holidays I could manage to have something working, but that's unsure...
    – manu
    Dec 18, 2014 at 15:04
  • Sounds good. Should be quite useful.
    – Drew
    Dec 18, 2014 at 15:57

2 Answers 2

0

I don't have an answer wrt XPath queries; sorry. But you might try Icicles search search keys M-s M-s x and M-s M-s X (commands icicle-search-xml-element and icicle-search-xml-element-text-node).

These let you search the contents and the text() nodes, respectively, of top-level XML elements whose names match a regexp that you provide.

For icicle-search-xml-element, can have any of these forms:

  • <ELEMENTNAME>...</ELEMENTNAME>
  • <ELEMENTNAME ATTRIBUTE1="..."...>...</ELEMENTNAME>
  • <ELEMENTNAME/>
  • <ELEMENTNAME ATTRIBUTE1="...".../>

You can alternatively choose to search, not the search contexts as defined by the element-name regexp, but the non-contexts, that is, the buffer text that is outside such elements. To do this, use `C-M-~' during completion. (This is a toggle, and it affects only future search commands, not the current one.)

For icicle-search-xml-element-text-node, the top-level matching elements must not have attributes. Only top-level elements of the form <ELEMENTNAME>...</ELEMENTNAME> are matched.

HTH.

2
  • 1
    I'm actually testing a long xpath expression against an XML. So there's no way around it. I have to test this xpath so I can put it in my code. Specifically I'm writing an extension to an OpenERP view (which is done via XML) and they way they provide to match places you want to change is via Xpath. So testing my expressions is what I need.
    – manu
    Dec 17, 2014 at 14:38
  • I understand. There really is no substitute for XPath (and XQuery) matching. (Certainly not regexps.)
    – Drew
    Dec 17, 2014 at 16:41
0

I did something like that a long time ago. I can't give you any details now, but I'll provide an overview of the approach I took.

I created some Emacs functions to interact with (query) a native XML database. I did it with a MarkLogic server once and with a Berkley DB XML database another time. One of those functions simply queried the database. Another one of the functions would send an XQuery query that included an Emacs buffer or buffer selection.

The native XML database server would process the query, return the results, and my Emacs functions would render the result in a result buffer.

This approach allowed me to query the XML with XPath and XQuery, which is a much more powerful query language that includes XPath. (I wrote about XQuery a long time ago, here: https://www.ibm.com/developerworks/library/x-xqueryxpath/)

As difficult as all of this might sound, it turned out to be surprisingly easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.