我需要从此 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 提取名称值的正确语法是什么?
注意:我已经有一个 solution使用 grep 和 sed,但我想使用 xmllint。
最佳答案
您的文件使用命名空间 (xmlns)。为了独立于这些,我建议:
xmllint --xpath "//*[local-name()='widget']/*[local-name()='name']/text()" config.xml
输出:
Product Finder
关于xml - 通过在 bash 中使用 xmllint 从具有命名空间的 xml 文件中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694722/