通过在bash中使用xmllint从具有名称空间的xml文件中提取值 [英] Extract value from xml file with namespaces by using xmllint in bash

查看:244
本文介绍了通过在bash中使用xmllint从具有名称空间的xml文件中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从此xml中提取名称值(产品搜索器):

I need to extract the name value (Product Finder) from this 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 问题我需要手动设置名称空间.所以我尝试了:

Which is probably because my config.xml file has other namespaces. According to this question I need to set the namespace by hand. So I've tried:

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

此上没有任何输出.用xmllint提取名称值的正确语法是什么?

There is no output on this one. Whats the correct syntax to extract the name value with xmllint?

注意:我已经有一个解决方案使用grep和sed,但我想使用xmllint.

Note: I already have a solution with grep and sed, but I would like to use xmllint.

推荐答案

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

Your file uses namespaces (xmlns). To be independent of these, I suggest:

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

输出:


Product Finder

这篇关于通过在bash中使用xmllint从具有名称空间的xml文件中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆