使用 xmllint 查找多个值
Look for more then one value using xmllint
我需要从 XML 文件中的多个 XML 块中检索多个值。我如何使用 xmllint 来执行此操作?
我注意到这个解决方案 (xml_grep get attribute from element) 并尝试扩展它。不幸的是,到目前为止没有任何运气。
xmllint --xpath 'string(//identity/@name @placeofbirth @photo)' file.xml
示例 XML 文件:
<eid>
<identity>
<name>Menten</name>
<firstname>Kasper</firstname>
<middlenames>Marie J</middlenames>
<nationality>Belg</nationality>
<placeofbirth>Sint-Truiden</placeofbirth>
<photo>base64-string</photo>
</identity>
<identity>
<name>Herbal</name>
<firstname>Jane</firstname>
<middlenames>Helena</middlenames>
<nationality>Frans</nationality>
<placeofbirth>Paris</placeofbirth>
<photo>notavailable</photo>
</identity>
</eid>
想要输出
Kasper, Sint-Truiden, base64-string
Jane, Paris, notavailable
一种方法是
# Read xml into variable
xmlStr=$(cat test.xml)
# Count identity nodes
nodeCount=$(echo "$xmlStr" | xmllint --xpath "count(//identity)" -)
# Iterate the nodeset by index
for i in $(seq 1 $nodeCount);do
echo "$xmlStr" | xmllint --xpath "concat((//identity)[$i]/name,', ',(//identity)[$i]/placeofbirth, ', ', (//identity)[$i]/photo)" - ; echo
done
结果:
Menten, Sint-Truiden, base64-string
Herbal, Paris, notavailable
我需要从 XML 文件中的多个 XML 块中检索多个值。我如何使用 xmllint 来执行此操作?
我注意到这个解决方案 (xml_grep get attribute from element) 并尝试扩展它。不幸的是,到目前为止没有任何运气。
xmllint --xpath 'string(//identity/@name @placeofbirth @photo)' file.xml
示例 XML 文件:
<eid>
<identity>
<name>Menten</name>
<firstname>Kasper</firstname>
<middlenames>Marie J</middlenames>
<nationality>Belg</nationality>
<placeofbirth>Sint-Truiden</placeofbirth>
<photo>base64-string</photo>
</identity>
<identity>
<name>Herbal</name>
<firstname>Jane</firstname>
<middlenames>Helena</middlenames>
<nationality>Frans</nationality>
<placeofbirth>Paris</placeofbirth>
<photo>notavailable</photo>
</identity>
</eid>
想要输出
Kasper, Sint-Truiden, base64-string
Jane, Paris, notavailable
一种方法是
# Read xml into variable
xmlStr=$(cat test.xml)
# Count identity nodes
nodeCount=$(echo "$xmlStr" | xmllint --xpath "count(//identity)" -)
# Iterate the nodeset by index
for i in $(seq 1 $nodeCount);do
echo "$xmlStr" | xmllint --xpath "concat((//identity)[$i]/name,', ',(//identity)[$i]/placeofbirth, ', ', (//identity)[$i]/photo)" - ; echo
done
结果:
Menten, Sint-Truiden, base64-string
Herbal, Paris, notavailable