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">

<activity android:label="@string/app_name" android:name="com.whatever.app">

<intent-filter>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="5" />
</manifest>
Elementtree expects namespace URIs, not namespace prefixes.
See: ElementTree: Working with Qualified Names
Edit The XPath implementation of node-elementtree does not currently seem to have namespace support at all.
Missing that you’d have to do some legwork:
The line
if ( activities[i].attrib['android:name'] === 'com.whatever.app' ) {is largely a guess.I don’t know how the parser handles namespaced attributes. When in doubt, just dump the whole
activities[i].attribto the console and see what the parser did. Adapt the above code accordingly. I’m afraid that’s as close as you will get with that kind of limited XPath support.