使用 xmlStarlet 工具在 XML 文件中获取值

get value in an XML file with xmlStarlet tool

我的XML文件结构

<properties>
    <structure name="preference">
        <structure name="cam">
            <property name="number" value="8"/>
            <property name="name" value="eddi"/>
        </structure>
    </structure>
</properties>

使用 XMLstarlet 工具,我试图获取值“8”,所以我首先尝试了以下命令:

xmlstarlet -t -v '/properties/structure[@name="preference"]/structure[@name="cam"][property/@name="number"]/@value' file.xml

然后我得到这些错误:

parser error : attributes construct error
xsl:value-of select="/properties/structure[@name="preference"]

parser error : Couldn't find end of Start Tag value-of line 23
xsl:value-of select="/properties/structure[@name="preference"]

这些错误指向 "preference"。
我看到了一些处理此类操作的主题,但他们的解决方案与我的情况不符。

你知道如何获得这个值吗?

对于更新(@har07)和有效 xml,例如:

<properties>
    <structure name="preference">
        <structure name="cam">
            <property name="number" value="8"/>
            <property name="name" value="eddi"/>
        </structure>
    </structure>
</properties>

(注意自闭标签 />

你可以使用:

xmlstarlet sel -T -t -m '//property[@name="number"]/@value' -v . -n file.xml

并打印

8

使用:

xmlstarlet --version
1.6.1
compiled against libxml2 2.9.4, linked with 20904
compiled against libxslt 1.1.29, linked with 10129