指定パスの複数の要素を取り出したい:cxml stp - lisp-cookbook-ja/common-lisp GitHub Wiki

このページでは、CLiki:plexippus-xpathを組み合せる例を紹介しています

ライブラリの導入方法

(ql:quickload :xpath)

XML文章構築

(defparameter *doc*
  (cxml:parse
   "<a><b><c>1</c></b><b><c>2</c></b><b><c>3</c><d>4</d></b></a>"
   (stp:make-builder)))

xpathで/a/b/cを探す

(mapcar #'stp:string-value
        (xpath:all-nodes (xpath:evaluate "/a/b/c" *doc*)))
;=>  ("1" "2" "3")

xpathは使わず、cxml-stpパッケージの関数のみでの実現

(labels ((fc (name parent)
           (if (listp parent)
               (mapcan (lambda (x) (fc name x))
                       parent)
               (remove-if-not (lambda (x) (string= x name))
                              (stp:list-children parent)
                              :key #'stp:local-name))))
  (mapcar #'stp:string-value
          (fc "c" (fc "b" (fc "a" *doc*)))))
;=>  ("1" "2" "3")
⚠️ **GitHub.com Fallback** ⚠️