#author("2021-02-27T04:34:00+00:00","default:hotate","hotate")
#author("2021-02-27T05:07:22+00:00","default:hotate","hotate")
#contents
&tag(XML,シェル);

* 情報 [#u764e22d]
** 概要 [#dad56a0e]
- [[【Shell】XMLファイルから要素や属性を抽出する - マイナー・マイナー>https://minor.hatenablog.com/entry/2016/01/12/222513]]
 XMLファイルから特定の要素や属性を抽出して整形する方法をまとめました。Shellのgrep、sed、xmllintコマンドを組み合わせて、要素や属性を取得します。
-- コメント:[[xmllint]]の出力機能が弱い為、改行処理等を[[sed]]で補っている。(2021/02/27)
- [[ShellScriptでXMLの内容を取り出す |>http://labs.opentone.co.jp/?p=5273]]
 「sample」→「food」→「name」要素の内容を取得する場合は、下記のように指定します。
 / > cat /sample/food/name

** 名前空間 [#k8a45114]
- [[xml - xmllint failing to properly query with xpath - Stack Overflow>https://stackoverflow.com/questions/8264134/xmllint-failing-to-properly-query-with-xpath]]
 A. Use xmllint in shell mode and declare the namespace with a prefix. You can then use that prefix in your XPath.
 B. Use local-name() to match element names.
   xmllint --xpath /*[local-name()='chat']
- [[Extract value from xml file with namespaces by using xmllint in bash - Stack Overflow>https://stackoverflow.com/questions/43694722/extract-value-from-xml-file-with-namespaces-by-using-xmllint-in-bash]]
 This means you also need to use the prefix x to reference name element since it inherits the default namespace from widget :
 / > xpath /x:widget/x:name/text()[1]

* 関連 [#k8c0878e]
#related

#taglist(tag=XML)

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS