0
我正在嘗試構建一個將在XPath中正確引用/轉義屬性的函數。我已經看到張貼在C#here和here解決方案,但我在JavaScript執行導致錯誤「這種表達是不合法的表達」JavaScript中的XPath屬性引用
這裏是我的功能:
function parseXPathAttribute(original){
let result = null;
/* If there are no double quotes, wrap in double quotes */
if(original.indexOf("\"")<0){
result = "\""+original+"\"";
}else{
/* If there are no single quotes, wrap in single quotes */
if(original.indexOf("'")<0){
result = "'"+original+"'";
}else{ /*Otherwise, we must use concat() */
result = original.split("\"")
for (let x = 0;x<result.length;x++){
result[x] = result[x].replace(/"/g,"\\\"");
if (x>0){
result[x] = "\\\""+result[x];
}
result[x] = "\""+result[x]+"\"";
}
result = result.join();
result = "concat("+result+")";
}
}
return result;
}
樣品未能輸入:
「 '喜'」
樣品失敗的輸出:
CONCAT( 「」, 「\」 '喜' 「 」\「」)]
我不明白爲什麼它是一個非法的表達式(因爲雙引號被轉義),所以我不知道如何修復這個功能。
我開發使用Mozilla的平臺客戶端應用程序。這不適用於瀏覽器。謝謝。 – pc1oad1etter 2010-08-06 16:51:34