I want to get the value of name and put it in a variable using XMLLint
我想获取name的值,并使用XMLLint将其放入一个变量中
<body>
<value name="abc"></value>
</body>
echo 'cat //body/value/@name' | xmllint --shell "test.xml"
/ > -------
name="abc"
/ >
So I want to assign the value "abc" to variable $test
所以我想把“abc”的值赋给变量$test
4 个解决方案
#1
24
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
.
您需要使用fn:string(),它将将其参数的值返回为xs:string。如果它的参数是一个属性,那么它将返回属性的值为xs:string。
test=$(xmllint --xpath "string(//body/value/@name)" test.xml)
#2
8
Try this, it's not beautiful but it works :)
试试这个,它不漂亮,但很管用:
I just erase lines containing >
from stdout , cut the string to get the second part after the =
, and delete "
我只是擦掉了从stdout中包含>的行,将字符串切成=,然后删除"
test=$(echo 'cat //body/value/@name' | xmllint --shell "test.xml" | grep -v ">" | cut -f 2 -d "=" | tr -d \");
echo $test
#3
4
I recently had to port my original simpler solution using --xpath to a platform lacking this feature, so had to adopt the "cat" solution too. This will handle multiple matches, tested on Ubuntu 12.04 and Solaris 11:
我最近不得不将我最初使用的更简单的解决方案(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'
}
e.g. extracting instance names from a glassfish domain config:
例如,从glassfish域配置中提取实例名:
$ getxml /tmp/test.xml "//server[@node-ref]/@name"
inst1
inst2
The sed post-processing just grabs all quoted values which was adequate for my needs (getting bits of glassfish config).
sed后处理只获取所有引用的值,这些值足以满足我的需要(获取少量的glassfish配置)。
#4
3
An approach with a helper awk
command that supports multiple attributes (a streamlined version of ego's approach):
一种支持多个属性的助手awk命令的方法(自我方法的简化版本):
echo 'cat //*/@name' | xmllint --shell file | awk -F\" 'NR % 2 == 0 { print $2 }'
The awk
command:
awk命令:
-
splits
xmllint
's output lines into fields by"
chars. (-F\"
)将xmllint的输出行拆分为“chars”。(- f \”)
- Note that
xmllint
normalizes quoting around attribute values to"..."
on output, even if the input had'...'
, so it's sufficient to split by"
. - 注意,xmllint在输出时将引用的属性值规范化为“…”,即使输入是“…”",所以分裂就足够了"
- Note that
-
only processes even-numbered lines (
NR %2 == 0
), thereby filtering out the separator lines thatcat
invariably prints.只处理偶数编号的行(NR %2 == 0),因此过滤掉cat总是打印的分隔线。
-
print $2
then prints only the 2nd field, which is the value of each attribute without the enclosing"..."
.打印$2然后只打印第2个字段,这是每个属性的值,不包含“…”。
Assuming the following sample XML in file
:
假设文件中有以下示例XML:
<body>
<value name="abc"></value>
<value name="def"></value>
</body>
the above yields:
上述收益率:
abc
def
#1
24
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
.
您需要使用fn:string(),它将将其参数的值返回为xs:string。如果它的参数是一个属性,那么它将返回属性的值为xs:string。
test=$(xmllint --xpath "string(//body/value/@name)" test.xml)
#2
8
Try this, it's not beautiful but it works :)
试试这个,它不漂亮,但很管用:
I just erase lines containing >
from stdout , cut the string to get the second part after the =
, and delete "
我只是擦掉了从stdout中包含>的行,将字符串切成=,然后删除"
test=$(echo 'cat //body/value/@name' | xmllint --shell "test.xml" | grep -v ">" | cut -f 2 -d "=" | tr -d \");
echo $test
#3
4
I recently had to port my original simpler solution using --xpath to a platform lacking this feature, so had to adopt the "cat" solution too. This will handle multiple matches, tested on Ubuntu 12.04 and Solaris 11:
我最近不得不将我最初使用的更简单的解决方案(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'
}
e.g. extracting instance names from a glassfish domain config:
例如,从glassfish域配置中提取实例名:
$ getxml /tmp/test.xml "//server[@node-ref]/@name"
inst1
inst2
The sed post-processing just grabs all quoted values which was adequate for my needs (getting bits of glassfish config).
sed后处理只获取所有引用的值,这些值足以满足我的需要(获取少量的glassfish配置)。
#4
3
An approach with a helper awk
command that supports multiple attributes (a streamlined version of ego's approach):
一种支持多个属性的助手awk命令的方法(自我方法的简化版本):
echo 'cat //*/@name' | xmllint --shell file | awk -F\" 'NR % 2 == 0 { print $2 }'
The awk
command:
awk命令:
-
splits
xmllint
's output lines into fields by"
chars. (-F\"
)将xmllint的输出行拆分为“chars”。(- f \”)
- Note that
xmllint
normalizes quoting around attribute values to"..."
on output, even if the input had'...'
, so it's sufficient to split by"
. - 注意,xmllint在输出时将引用的属性值规范化为“…”,即使输入是“…”",所以分裂就足够了"
- Note that
-
only processes even-numbered lines (
NR %2 == 0
), thereby filtering out the separator lines thatcat
invariably prints.只处理偶数编号的行(NR %2 == 0),因此过滤掉cat总是打印的分隔线。
-
print $2
then prints only the 2nd field, which is the value of each attribute without the enclosing"..."
.打印$2然后只打印第2个字段,这是每个属性的值,不包含“…”。
Assuming the following sample XML in file
:
假设文件中有以下示例XML:
<body>
<value name="abc"></value>
<value name="def"></value>
</body>
the above yields:
上述收益率:
abc
def