proglog

主にプログラミングに関する断片的メモ

sxml、sxpathの使い方をメモ

  • そのクロージャ−は結果を、sxmlのリストで返す。
  • ヒットするものが一つでも。
  • 式がヒットしなかった場合は()が返る。
(use sxml.ssax)
(use sxml.sxpath)

(define xml-data
  "<html>
     <body>
        <h1>xml xmpath test</h1>
        <p class='para'>this is a test data.
          <div id='listdata'>
             <span id='one'>one text data</span>
             <span id='two'>two text data</span>
             <span id='three'>three text data</span>
          </div>
         </p>
        <p class='para'>paragraph 2</p>
       </body>
     </html>")

(call-with-input-string xml-data
  (lambda (pin)
    (let ((sxml-data (ssax:xml->sxml pin '())))
      (format #t "~A~%"  ((sxpath '(html body h1)) sxml-data))
      (newline)
      (format #t "~A~%"  ((sxpath "//p[@class='para']") sxml-data))
      (newline)
      (format #t "~A~%"  ((sxpath "//span[@id='two']") sxml-data))
      (newline)
      (format #t "~A~%"  ((sxpath "//span[@id='four']") sxml-data))
      (newline)
      )))

call-with-input-stringを使ってるのは、saxで変換するから、逐次読み出しでって感じか。

出力は下のような感じ。
ちょっと整形していある。

((h1 xml xmpath test))

((p (@ (class para)) this is a test data.
           (div (@ (id listdata))
		(span (@ (id one)) one text data)
		(span (@ (id two)) two text data)
		(span (@ (id three)) three text data)))
 (p (@ (class para)) paragraph 2))

((span (@ (id two)) two text data))

()

参考
[http://karetta.jp/article/blog/oneline/007798:title= Karetta|[Gauche] RSSを読み込んでSXMLに変換する]
[http://karetta.jp/article/blog/oneline/007816:title= Karetta|[Gauche] SXPathを使ってRSSから要素を取り出す]
Gauche:SXMLとSXPath