最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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=".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="">
    <application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">&#xA; 
        <activity android:label="@string/app_name" android:name=".whatever.app">&#xA;                
            <intent-filter>&#xA;</intent-filter>
        </activity> 
    </application>
    <uses-sdk android:minSdkVersion="5" />
</manifest>

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=".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./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=".whatever.app">&#xA;                
            <intent-filter>&#xA;</intent-filter>
        </activity> 
    </application>
    <uses-sdk android:minSdkVersion="5" />
</manifest>
Share Improve this question edited Jun 20, 2012 at 22:33 wildabeast asked Jun 20, 2012 at 21:33 wildabeastwildabeast 1,8323 gold badges19 silver badges34 bronze badges 1
  • Have you tried escaping the colon \:? – mVChr Commented Jun 20, 2012 at 21:37
Add a ment  | 

2 Answers 2

Reset to default 8

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./apk/res/android'
      ] 
   = 
    '.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'] = '.whatever.app']

or this expression:

application/activity[@*[name()='android:name'] = '.whatever.app']

Elementtree expects namespace URIs, not namespace prefixes.

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

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

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:

var manifestTxt = fs.readFileSync('AndroidManifest.xml', 'utf-8'),
    manifestDoc = new et.ElementTree(et.XML(manifestTxt)),
    activities = manifestDoc.findall('//application/activity'), i;

for (i=0; i<activities.length; i++) {
  if ( activities[i].attrib['android:name'] === '.whatever.app' ) {
    test.ok(true);
  }
}

The line if ( activities[i].attrib['android:name'] === '.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].attrib to 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论