【发布时间】:2017-09-27 10:18:10
【问题描述】:
我需要从这个 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 的 solution,但我想使用 xmllint。
【问题讨论】:
-
我可以用 xmlstarlet 轻松解决这个问题
-
sed 's/xmlns=".*"//g' config.xml | xmllint --xpath '/widget/name/text()' -虽然我支持赛勒斯的回答 :-)
标签: xml bash xml-namespaces xmllint