unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Digging information out of parsed XML
@ 2003-05-14 15:35 Mark A. Hershberger
  0 siblings, 0 replies; only message in thread
From: Mark A. Hershberger @ 2003-05-14 15:35 UTC (permalink / raw)



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:

  <top>
    <middle>
      <bottom>3<stop/>4</bottom>
    </middle>
    <middle>
      <bottom>5</bottom>
    </middle>
  </top>

xml.el turns this into something like

  ((top nil 
      (middle nil 
	      (bottom nil "3"
		      (stop nil
			    (""))
		      "4"))
      (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."
  (cond 
   ((stringp xml)
    (when (string-equal (substring path 0 7) "/text()")
      xml))
    
   ((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)
		(t
		 (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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-05-14 15:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-14 15:35 Digging information out of parsed XML Mark A. Hershberger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).