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



我的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

然后我收到这些错误:

解析器错误:属性构造错误
xsl:value-of select="/properties/structure[@name="ppreferences"]

解析器错误:找不到第 23
行的开始标记值的结尾 xsl:value-of select="/properties/structure[@name="ppreferences"]

这些错误指向"首选项"。
我看到一些涉及此类操作的主题,但它们的解决方案与我的情况不匹配。

您对如何获得此值有任何想法吗?

对于更新的(按 @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