Search code examples
androidxmlxmllint

Get attribute value from xml with xmllint


I have AndroidManifest.xml file and i wand get version code and name with xmllint I try command like

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

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

and I don't have any in output

if I type

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

I get package name.

So how can I get value from attribute that contains ":"?

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>

Solution

  • You have to specify the namespace-uri and use local-name to get the name of the attribute without the namespace prefix:

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