在属性名称中使用冒号搜索xpath表达式会引发异常(node.js elementtree模块) [英] Searching for xpath expression with colon in attribute name throws exception (node.js elementtree module)

查看:234
本文介绍了在属性名称中使用冒号搜索xpath表达式会引发异常(node.js elementtree模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用nodejs中的elementtree包,我试图验证xml文件(特别是android清单文件)中是否存在某个xml属性。

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));

我遇到以下异常:

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.

编辑搜索:

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


推荐答案

Elementtree需要名称空间URI,而不是名称空间前缀。

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.com/apk/res/android}name="com.whatever.app"]';

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

请参阅:元素树:使用限定名称

编辑 node-elementtree 的XPath实现没有当前似乎完全支持名称空间。

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'] === 'com.whatever.app' ) {
    test.ok(true);
  }
}

if( [i] .attrib ['android:name'] ==='com.whatever.app'){基本上是一个猜测。

The line if ( activities[i].attrib['android:name'] === 'com.whatever.app' ) { is largely a guess.

我不知道解析器如何处理命名空间属性。如有疑问,只需将整个 activities [i] .attrib 转储到控制台,看看解析器做了什么。相应地修改上面的代码。恐怕只有有限的XPath支持,您才能与之接近。

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.

这篇关于在属性名称中使用冒号搜索xpath表达式会引发异常(node.js elementtree模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆