如何使用 xmllint 和 XPath 从属性中获取值? [英] How can I get the value from an attribute using xmllint and XPath?

查看:69
本文介绍了如何使用 xmllint 和 XPath 从属性中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 name 的值并使用 XMLLint 将其放入变量

I want to get the value of name and put it in a variable using XMLLint

<body>
<value name="abc"></value>
</body>

echo 'cat //body/value/@name' | xmllint --shell "test.xml"

/ >  -------
 name="abc"
/ > 

所以我想将值abc"赋给变量 $test

So I want to assign the value "abc" to variable $test

推荐答案

您需要使用 fn:string(),它将以 xs:string 形式返回其参数的值.如果它的参数是一个属性,那么它将返回属性的值作为 xs:string.

You need to use fn:string(), which will return the value of its argument as xs:string. In case its argument is an attribute, it will therefore return the attribute's value as xs:string.

test=$(xmllint --xpath "string(//body/value/@name)" test.xml)

这篇关于如何使用 xmllint 和 XPath 从属性中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆