Extract value from xml in shell

人走茶凉 提交于 2020-01-24 21:55:26

问题


I have to following xml structure:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DOC SYSTEM "ts.dtd">
<?xml-stylesheet type="text/css" href="ts.css"?>
<DOC LOCALE="en-US"> 
   <PTXT ID="some.first.id" CONTEXT="">Some text 1</PTXT> 
   <PTXT ID="some.second.id" CONTEXT="">Some text 2</PTXT> 
</DOC>

Now my challenge is to loop on every PTXT tag and do something with the ID and the inner text. For example purposes, let's just say that I need to echo something like

some.first.id Some text 1
some.second.id Some text 2

How can I have that in a shell script?


回答1:


Complete solution with xmlstarlet tool:

xmlstarlet sel -t -m "//PTXT" -v "concat(./@ID,' ',text())" -n input.xml 2>/dev/null

The output:

some.first.id Some text 1
some.second.id Some text 2


来源:https://stackoverflow.com/questions/47393839/extract-value-from-xml-in-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!