![](/img/trans.png)
[英]Get value of an attribute from xml for multiple occurrences using xmllint
[英]How can I get the value from an attribute using xmllint and XPath?
我想获取名称的值并使用 XMLLint 将其放入变量中
<body>
<value name="abc"></value>
</body>
echo 'cat //body/value/@name' | xmllint --shell "test.xml"
/ > -------
name="abc"
/ >
所以我想将值“abc”分配给变量 $test
您需要使用fn:string() ,它将返回其参数的值为xs:string
。 如果它的参数是一个属性,它将返回属性的值为xs:string
。
test=$(xmllint --xpath "string(//body/value/@name)" test.xml)
试试这个,它不漂亮,但它的工作:)
我只是从stdout中删除包含>
行,剪切字符串以获取=
后面的第二部分,然后删除“
test=$(echo 'cat //body/value/@name' | xmllint --shell "test.xml" | grep -v ">" | cut -f 2 -d "=" | tr -d \");
echo $test
使用helper awk
命令支持多个属性 的方法 ( 自我方法的简化版本):
echo 'cat //*/@name' | xmllint --shell file | awk -F\" 'NR % 2 == 0 { print $2 }'
awk
命令:
通过"
chars。(- xmllint
-F\\"
)将xmllint
的输出行拆分为字段
'...'
, xmllint
将输出上属性值的引用规范化为"..."
,因此它足以按"
分割"
。 仅处理偶数行( NR %2 == 0
),从而滤除cat
总是打印的分隔线。
print $2
然后只打印第二个字段,这是每个属性的值 ,没有封闭的"..."
。
假设file
包含以下示例XML:
<body>
<value name="abc"></value>
<value name="def"></value>
</body>
以上产量:
abc
def
我最近不得不将使用--xpath的原始简单解决方案移植到缺少此功能的平台,因此不得不采用“cat”解决方案。 这将处理在Ubuntu 12.04和Solaris 11上测试的多个匹配:
getxml() { # $1 = xml file, $2 = xpath expression
echo "cat $2" | xmllint --shell $1 |\
sed -n 's/[^\"]*\"\([^\"]*\)\"[^\"]*/\1/gp'
}
例如,从glassfish域配置中提取实例名称:
$ getxml /tmp/test.xml "//server[@node-ref]/@name"
inst1
inst2
sed后处理只是抓取所有引用的值,这些值足以满足我的需求(获得一些glassfish配置)。
为了匹配多行并提取其同名属性的值并建立在以前的答案的基础上,这对我有用,而且更短,呵呵
xmllint --xpath "//*/@value" file.xml | awk -F\" '{ print $2 }'
下一个对我有用:
xmllint --xpath '//o/@loc' file.xml | sed -n 's/[^\"]*\"\([^\"]*\)\"[^\"]*/\1\n/gp'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.