我有这个xml模式,我想要的是如何使用XMLStarlet在shell脚本中一个接一个地提取所有节点的值
<service>
<imageScroll>
<imageName>Photo_Gallerie_1.jpg</imageName>
</imageScroll>
<imageScroll>
<imageName>Photo_Gallerie_2.jpg</imageName>
</imageScroll>
<imageScroll>
<imageName>Photo_Gallerie_3.jpg</imageName>
</imageScroll>
</service>
Run Code Online (Sandbox Code Playgroud)
小智 6
xmlstarlet sel -t -m "//imageName" -v . -n your.xml
Run Code Online (Sandbox Code Playgroud)
输出:
Photo_Gallerie_1.jpg
Photo_Gallerie_2.jpg
Photo_Gallerie_3.jpg
Run Code Online (Sandbox Code Playgroud)
那是你需要的吗?
sel (选择模式)-t (输出模板(这是非常必要的)-m(对于以下值的每次匹配)
"// (双斜杠表示它可以在树中的任何位置)imageName (您想要的节点名称)”-v(请求当前路径中元素的值),并.在迭代中表示当前元素(您可以将节点的名称放在此处,但通常这样更容易),然后-n 是为您匹配的每个值添加一行。这就是我找到的解决方案,它完美地完成了工作。
imagescroller=`xmlstarlet sel -t -m "//root/services/service/imageScroll[rank_of_the_desired_item]" -v imageName -n myfile.xml
Run Code Online (Sandbox Code Playgroud)
对不起我迟到了。