Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for xpath expression with colon in attribute name throws exception (node.js elementtree module)

Using the elementtree package in nodejs, I'm trying to verify the existence of a certain xml attribute in an xml file (specifically an android manifest file).

var manifestTxt = fs.readFileSync('AndroidManifest.xml', 'utf-8'),
    manifestDoc = new et.ElementTree(et.XML(manifestTxt)),
    expected = 'application/activity[@android:name="com.whatever.app"]';

test.ok(manifestDoc.find(expected));

I'm getting the following exception:

node_modules/elementtree/lib/elementpath.js:210
      throw new SyntaxError(token, 'Invalid attribute predicate');
        ^
Error: Invalid attribute predicate

It doesn't seem to like the colon in the attribute name, but without it the search doesn't match. I think I'm handling the namespace wrong -- but can't find the proper way.

Edit Here's the sample xml I'm searching:

<?xml version='1.0' encoding='utf-8'?>
<manifest ... xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">&#xA; 
        <activity android:label="@string/app_name" android:name="com.whatever.app">&#xA;                
            <intent-filter>&#xA;</intent-filter>
        </activity> 
    </application>
    <uses-sdk android:minSdkVersion="5" />
</manifest>
like image 902
wildabeast Avatar asked Aug 06 '26 11:08

wildabeast


1 Answers

In case you don't have the information how to register the namespace and use the associated prefix for it, use:

application/activity
   [@*[local-name()=name' 
     and 
      namespace-uri() = 'http://schemas.android.com/apk/res/android'
      ] 
   = 
    'com.whatever.app'
   ]

Simpler expressions that aren't safe in the general case, but may select the wanted node(s) in this specific case:

application/activity[@*[local-name()='name'] = 'com.whatever.app']

or this expression:

application/activity[@*[name()='android:name'] = 'com.whatever.app']
like image 121
Dimitre Novatchev Avatar answered Aug 08 '26 02:08

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!