在 bash 中使用 xmllint 从具有命名空间的 xml 文件中提取值

Extract value from xml file with namespaces by using xmllint in bash

我需要从这个 xml 中提取名称值(产品查找器):

文件:config.xml

<?xml version="1.0" encoding="utf-8"?>
<widget id="com.abc.app" version="1.3.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0" ios-CFBundleVersion="1.3.1.5" android-versionCode="5">
    <name>Product Finder</name>
    <description>
        Description
    </description>
</widget>

我试过:

mles$ xmllint --xpath "/widget/name/text()" config.xml 
XPath set is empty

这可能是因为我的 config.xml 文件有其他命名空间。根据 this 问题,我需要手动设置命名空间。所以我试过了:

mles$ xmllint --shell config.xml / > setns x=http://www.w3.org/ns/widgets / > xpath /x:widget/name/text

这个没有输出。使用 xmllint 提取名称值的正确语法是什么?

注意:我已经有一个带有 grep 和 sed 的 ,但我想使用 xmllint。

请注意,不仅声明默认命名空间的元素驻留在该命名空间中,而且所有没有前缀和本地默认命名空间的后代元素都隐式继承了祖先的默认命名空间。这意味着您还需要使用前缀 x 来引用 name 元素,因为它继承了 widget 的默认名称空间:

/ > xpath /x:widget/x:name/text()[1]

您的文件使用了命名空间 (xmlns)。为了独立于这些,我建议:

xmllint --xpath "//*[local-name()='widget']/*[local-name()='name']/text()" config.xml

输出:

Product Finder