検索テキストを含むxmlノードを取得します。

検索テキストを含むxmlノードを取得します。

以下のようなフォーマットがよく指定され、インデントされたXMLがあります。テキストを含むモノラベルをインポートする必要があります。http://yahoo.com

<root>
<mono id="1">
<tag1>http://google.com</tag1>
</mono>
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="3">
<tag1>http://mysite.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
</root>

次のコマンドを使用しようとしています。

cat -n index.xml | sed -n "/root/,/root>/p" |grepp"http://yahoo.com

6  <tag1>http://yahoo.com</tag1>
12  <tag1>http://yahoo.com</tag1>

このような出力が必要です。しかし、私の検索テキストを含む単一のノードを取得する方法がわかりません。

<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>

ベストアンサー1

使用XMLスター:

$ xmlstarlet sel -t -c '//mono[tag1 = "http://yahoo.com"]' -nl file.xml
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono><mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>

XPathは、「値が呼び出される子ノードを持つすべての//mono[tag1 = "http://yahoo.com"]ノード」を意味します。mongo「...のコピーをください」を意味し、最後に出力の末尾に改行文字を挿入します。tag1http://yahoo.com-c-nl

idノードの次のものと一致しようとしています。

xmlstarlet sel -t -v '//mono[tag1 = "http://yahoo.com"]/@id' -nl file.xml

おすすめ記事