// XPath 式中の接頭辞のない名前テストに接頭辞 prefix を追加する // e.g. '//body[@class = "foo"]/p' -> '//prefix:body[@class = "foo"]/prefix:p' // http://nanto.asablo.jp/blog/2008/12/11/4003371 function addDefaultPrefix(xpath, prefix) { var tokenPattern = /([A-Za-z_\u00c0-\ufffd][\w\-.\u00b7-\ufffd]*|\*)\s*(::?|\()?|(".*?"|'.*?'|\d+(?:\.\d*)?|\.(?:\.|\d+)?|[\)\]])|(\/\/?|!=|[<>]=?|[\(\[|,=+-])|([@$])/g; var TERM = 1, OPERATOR = 2, MODIFIER = 3; var tokenType = OPERATOR; prefix += ':'; function replacer(token, identifier, suffix, term, operator, modifier) { if (suffix) { tokenType = (suffix == ':' || (suffix == '::' && (identifier == 'attribute' || identifier == 'namespace'))) ? MODIFIER : OPERATOR; } else if (identifier) { if (tokenType == OPERATOR && identifier != '*') token = prefix + token; tokenType = (tokenType == TERM) ? OPERATOR : TERM; } else { tokenType = term ? TERM : operator ? OPERATOR : MODIFIER; } return token; } return xpath.replace(tokenPattern, replacer); }