我想弄清楚,首先在特定標簽(文章標簽)中搜索術語,然后從文章標簽中的特定標簽中檢索值。
我可以從特定標簽中檢索值,
<article>
<author>
<name>Example Name 1</name>
<title>example title 2</title>
</author>
<title>article title 1</title>
<publicationDate>2022-02-12</publicationDate>
<text>blah1 blah1 blah1</text>
<reference>10000</reference>
</article>
<article>
<author>
<name>Example Name 2</name>
<title>example title 2</title>
</author>
<title>article title 1</title>
<publicationDate>2022-02-13</publicationDate>
<text>blah1 blah1 blah1</text>
<reference>10001</reference>
</article>
xmllint --xpath "string(//title)" file.xml
但是如何搜索然后檢索文章標簽中的值?每次都會有不同的參考編號,然后我需要從該特定參考中提取值。
謝謝您的幫助
uj5u.com熱心網友回復:
如果我正確理解您的意圖,您應該能夠使用包含您感興趣的參考號的 bash 變數來引數化您的 xpath 搜索字串。請注意,我將您的示例 XML 修改為包含在標簽中,因此您需要根據您的 XML 結構修改 xpath。
腳本內容:
#!/bin/bash
ref_no=${1:-10001}
src_xml=${2:-/tmp/foo/s.xml}
title=$(xmllint --xpath "string(/articles/article[reference=${ref_no}]/title)" "${src_xml}")
printf "Reference: %s, Title: %s\n" "${ref_no}" "${title}"
輸出:
$ ./script 10000
Reference: 10000, Title: article title 1
$ ./script 10001
Reference: 10001, Title: article title 2
為清楚起見,這里是我使用的測驗 XML:
<articles>
<article>
<author>
<name>Example Name 1</name>
<title>example title 2</title>
</author>
<title>article title 1</title>
<publicationDate>2022-02-12</publicationDate>
<text>blah1 blah1 blah1</text>
<reference>10000</reference>
</article>
<article>
<author>
<name>Example Name 2</name>
<title>example title 2</title>
</author>
<title>article title 2</title>
<publicationDate>2022-02-13</publicationDate>
<text>blah1 blah1 blah1</text>
<reference>10001</reference>
</article>
</articles>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491433.html
下一篇:bash中的$符號是什么意思?
