在nodejs中使用elementtree包,我試圖驗證xml文件中是否存在某個xml屬性(特別是一個android清單文件)。在屬性名稱中使用冒號搜索xpath表達式引發異常(node.js elementtree模塊)
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
它似乎不喜歡在屬性名結腸,但沒有它的搜索不匹配。我想我正在處理命名空間錯誤 - 但找不到正確的方法。
編輯這裏的示例XML我在尋找:
<?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>
你試過轉義冒號'\:'嗎? – mVChr
是的,沒有效果。 – wildabeast