それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

xmllint で xml をディレクトリみたいにcdして辿る

xmllint は便利ですね。XMLファイル中をコンソール出来る。んですね

XMLbashみたいに使えたらイイなと思ってた

ず〜〜っっと思ってた。XMLってツリー構造だから、cd したりファイルシステムみたいにたどれたらイイなと思った。

xmllint 調べたら --shell というシェルオプションを見つけて歓喜したわ。

--shell オプション

xmllint --shell http://takuya-1st.hatenablog.jp/rss

シェルプオプションを使うと

cd したり、pwd 出来たり便利すぎる。

やってみたらこんな感じ

xmllint --shell http://takuya-1st.hatenablog.jp/rss
rss > du
rss
  channel
    title
    link
    description
    lastBuildDate
    docs
    generator
    item
      title
      link
      description
      pubDate
      guid
    item
      title
      link
      description
      pubDate
      guid
rss >

同名のノードの場合

同名のノードが複数回登場する場合は、インデックスを付けてアクセスする。

channel > pwd
/rss/channel
channel > cd item[1]
item > pwd
/rss/channel/item[1]
item >

便利なコマンド

  • du tree コマンド的なもの
  • cat 中身を見る
  • pwd 現在のPathをxpath式で。
  • cd XMLノードを動く
  • ls そのまま
  • help コマンド一覧

readline使えないのが惜しい・・・

矢印キー、tab で補完とか、Ctrl+Rで履歴をたどるとか出来ない。 出来ないのかなぁそのうち調べる。

参考資料 help について

--shell 起動中にhelp を叩くと、このような表示になる。

item > help
    base         display XML base of the node
    setbase URI  change the XML base of the node
    bye          leave shell
    cat [node]   display node or current node
    cd [path]    change directory to path or to root
    dir [path]   dumps informations about the node (namespace, attributes, content)
    du [path]    show the structure of the subtree under path or the current node
    exit         leave shell
    help         display this help
    free         display memory usage
    load [name]  load a new document with name
    ls [path]    list contents of path or the current directory
    set xml_fragment replace the current node content with the fragment parsed in context
    xpath expr   evaluate the XPath expression in that context and print the result
    setns nsreg  register a namespace to a prefix in the XPath evaluation context
                 format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)
    setrootns    register all namespace found on the root element
                 the default namespace if any uses 'defaultns' prefix
    pwd          display current working directory
    whereis      display absolute path of [path] or current working directory
    quit         leave shell
    save [name]  save this document to name or the original name
    write [name] write the current node to the filename
    validate     check the document for errors
    relaxng rng  validate the document agaisnt the Relax-NG schemas
    grep string  search for a string in the subtree