繁体   English   中英

使用xmllint从xml获取属性值

[英]Get attribute value from xml with xmllint

我有AndroidManifest.xml文件,我想用xmllint获取版本代码和名称,然后尝试使用类似的命令

xmllint --xpath "string(//manifest/@android:versionCode)" AndroidManifest.xml

xmllint --xpath "string(//manifest/@android:versionName)" AndroidManifest.xml

而且我没有任何输出

如果我输入

xmllint --xpath "string(//manifest/@package)" AndroidManifest.xml

我得到包裹名称。

那么如何从包含“:”的属性中获取值呢?

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"    android:versionCode="5" android:versionName="1.0" package="com.mobile.android" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="19" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application android:allowBackup="true" android:label="@string/app_name">
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.mobile.android" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

您必须指定namespace-uri并使用local-name来获取没有名称空间前缀的属性名称:

xmllint --xpath '
    string(/manifest/@*[local-name()="versionName" 
        and namespace-uri()="http://schemas.android.com/apk/res/android"])
    ' AndroidManifest.xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM