gpt4 book ai didi

xml - 使用 emacs elisp 解析 XML 并查找嵌套属性

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:08 26 4
gpt4 key购买 nike

我有一些看起来像这样的 xml:

<grammar>
<l>
<f form="paradāra"><s stem=""/><m meaning="anothers wife; adultery"/></f>
<f form="abhimarśeṣu"><s stem="" meaning=""/><m meaning=""/></f>
<f form="pravṛttān"><s stem="" meaning=""/><m meaning=""/></f>
<f form="mahipatis"><s stem="" meaning=""/><m meaning=""/></f>
</l>
<l>
<f form="udvejana"><s stem="udvejana" meaning="agitation, fear"/><m meaning=""/></f>
<f form="karais"><na><ins/><pl/><mas/></na><s stem="kara#1" meaning="action"/><m meaning="by action"/></f>
<f form="daṇḍais"><na><ins/><pl/><mas/></na><na><ins/><pl/><neu/></na><s stem="daṇḍa" meaning="punishment"/><m meaning="by punishment"/></f>
<f form="cihnayitvā"><s stem="" meaning="having marked"/><m meaning=""/></f>
<f form="pravāsayet"><v><cj><ca/></cj><sys><prs><md><op/></md><para/></prs></sys><np><sg/><trd/></np></v><s stem="pravas"/><m meaning="to put on, dress"/></f>
</l>
</grammar>

现在我通过运行 (xml-parse-region) 将其转换为 S 表达式。它返回如下内容:

((grammar nil "
" (l nil "
" (f ((form . "paradāra")) (s ((stem . ""))) (m ((meaning . "anothers wife; adultery")))) "

" (f ((form . "abhimarśeṣu")) (s ((stem . "") (meaning . ""))) (m ((meaning . "")))) "
" (f ((form . "pravṛttān")) (s ((stem . "") (meaning . ""))) (m ((meaning . "")))) "
" (f ((form . "mahipatis")) (s ((stem . "") (meaning . ""))) (m ((meaning . "")))) "
") "
" (l nil "
" (f ((form . "udvejana")) (s ((stem . "udvejana") (meaning . "agitation, fear"))) (m ((meaning . "")))) "

" (f ((form . "karais")) (na nil (ins nil) (pl nil) (mas nil)) (s ((stem . "kara#1") (meaning . "action"))) (m ((meaning . "by action")))) "

" (f ((form . "daṇḍais")) (na nil (ins nil) (pl nil) (mas nil)) (na nil (ins nil) (pl nil) (neu nil)) (s ((stem . "daṇḍa") (meaning . "punishment"))) (m ((meaning . "by punishment")))) "

" (f ((form . "cihnayitvā")) (s ((stem . "") (meaning . "having marked"))) (m ((meaning . "")))) "

" (f ((form . "pravāsayet")) (v nil (cj nil (ca nil)) (sys nil (prs nil (md nil (op nil)) (para nil))) (np nil (sg nil) (trd nil))) (s ((stem . "pravas"))) (m ((meaning . "to put on, dress")))) "

") "
"))

我现在要做的是提取所有以 (s ... ) 开头的子节点,并将它们收集到一个单独的缓冲区中。喜欢:

(s ((stem . "udvejana") (meaning . "agitation, fear")))

代码会是什么样子?递归遍历树?昨天我走到了第一个 (l ... ) 节点,但由于停电,我丢失了代码。希望你们中的一些人有一些建议!

最佳答案

你只需要基本的递归:

(defun rec-filter (predicate seq &optional acc)
(cond ((null seq)
acc)
((consp seq)
(append (rec-filter predicate (car seq) nil)
(rec-filter predicate (cdr seq) nil)
(if (funcall predicate seq)
(cons seq acc)
acc)))
(t
acc)))

(rec-filter
(lambda (x) (eq (car x) 's))
tree)
;; =>
;; ((s ((stem . "")))
;; (s ((stem . "")
;; (meaning . "")))
;; (s ((stem . "")
;; (meaning . "")))
;; (s ((stem . "")
;; (meaning . "")))
;; (s ((stem . "udvejana")
;; (meaning . "agitation, fear")))
;; (s ((stem . "kara#1")
;; (meaning . "action")))
;; (s ((stem . "daṇḍa")
;; (meaning . "punishment")))
;; (s ((stem . "")
;; (meaning . "having marked")))
;; (s ((stem . "pravas"))))

关于xml - 使用 emacs elisp 解析 XML 并查找嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28478235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com