[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Digging information out of parsed XML

From: Mark A. Hershberger
Subject: Digging information out of parsed XML
Date: Wed, 14 May 2003 10:35:05 -0500
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3 (gnu/linux)

I'm trying to build a parser that can handle some simple XPath
statements for XML parsed by xml.el.

For example, given the following XML:


xml.el turns this into something like

  ((top nil 
      (middle nil 
              (bottom nil "3"
                      (stop nil
      (middle nil
              (bottom nil "5"))))

I want to evaluate the XPath expression "/tob/middle/bottom/text()"
and have it return

    ("3" "4" "5")

However, what I get is 

    ((("3" "4")) (("5")))

Can someone help me pull those out into a nice list?

Here is xpath-eval:

(defun xpath-eval (xml path)
  "Given XML and PATH, return the results of the XPath query."
   ((stringp xml)
    (when (string-equal (substring path 0 7) "/text()")
   ((listp (car xml))
    (xpath-eval (car xml) path))

   ((string-equal (substring path 0 1) "/")
    (let* ((pos (string-match "/" path 1))
           (search-for (intern
                        (substring path 1 pos))))
      (when (eq (car xml) search-for)
          (cond ((not pos) xml)
                 (delq nil (mapcar 
                  (lambda (slice)
                      (xpath-eval slice
                                  (substring path pos)))
                  (cddr xml))))))))))

As long as you have mystery you have health; when you destroy mystery
you create morbidity.                        -- G.K. Chesterson

reply via email to

[Prev in Thread] Current Thread [Next in Thread]