[Top][All Lists]

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

Re: Problems with xml-parse-string

From: Lars Magne Ingebrigtsen
Subject: Re: Problems with xml-parse-string
Date: Fri, 24 Sep 2010 21:06:52 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux)

Chong Yidong <address@hidden> writes:

>> To take a concrete example: You want the src of the img node you have.
>> xml.el:  (cdr (assq 'img (cadr node)))
>> sxml.el: (if (and (consp (cadr node))
>>                   (eq (caadr node) '@))
>>              (cadr (assq 'img node)))
>> (And I'm not even sure that's correct.  It's probably not.  Which is my
>> point.)
>> libxml: (cdr (assq :img (cdr node)))
>> (The difference between libxml and xml.c for attributes is minuscule.)
> I think this example is confused.  If you're scanning at top-level,
> without descent, the code for all three cases is practically identical:
> it's a simple assq.

Sorry, "img" in the examples should be replaced with "src".

But I don't understand what you mean with "top-level".  This example was
about getting an attribute.

Chong Yidong <address@hidden> writes:

> I think that code might just be crufty.  Here's an implementation,
> assuming sxml format:
>   (defun nnrss-find-el (tag data &optional found-list)
>    "Find the all matching elements in the data.
>   Careful with this on large documents!"
>    (nreverse (nnrss-find-el-1 tag data)))
>   (defun nnrss-find-el-1 (tag data &optional found-list)
>     (and (consp data)
>          (not (eq (car data) '@))
>          (if (equal tag (car data))
>              (push data found-list)
>            (dolist (bit (cdr data))
>              (setq found-list
>                    (nnrss-find-el-1 tag bit found-list)))))
>     found-list)

Here's the libxml version:

(defun nnrss-find-el (tag node)
  (let (result)
    (dolist (elem (cdr node))
      (when (eq (car elem) tag)
        (push elem result))
       (when (consp (cdr elem))
         (setq result (nconc (nnrss-find-el tag elem) result))))

(Of course, if we were allowed to use CL constructions, it would be
something like (untested, because I have to run):

(defun nnrss-find-el (tag node)
  (loop for elem in (cdr node)
        if (eq (car elem) tag)
        collect elem
        when (consp (cdr elem))
        append (nnrss-find-el tag node)))

Can get much clearer than that, can it?        
> Doesn't seem too horrific.  Here's the example tree:

It's not too horrific.  It's just that it's not as nice as it could be.
And if you deal with these structures all the time, you get to recreate
the (and (consp data) (not (eq (car data) '@))) in every single branch
of every single little trivial function you write (and read).

>   (setq test-sxml-tree
>         '(tag (@ (attr1 "value1")
>                  (attr2 "value2"))
>               "Free text"
>               (foo "Text node 1")
>               (bar "Text node 2")
>               (baz
>                 "More free text"
>                 (foo "Text node 3")
>                 (bar "Text node 4"))))

(setq test-libxml-tree
        (:attr1 . "value1")
        (:attr2 . "value2")
        (text . "Free text")
        (foo (text . "Text node 1"))
        (bar (text . "Text node 2"))
         (text . "More free text")
         (foo (text . "Text node 3"))
         (bar (text . "Text node 4")))))
(domestic pets only, the antidote for overdose, milk.)
  address@hidden * Lars Magne Ingebrigtsen

reply via email to

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