Re: Keyboard Navigation Code - XPathEvaluate - need help
Martin Honnen <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.xml |
|---|---|
| Organization | Liberty Development |
| Message-ID | <ctqkps$agr3__6851.37642217359$1107351500$gmane$org@ripley.netscape.com> |
Ady Shimony wrote:
> My XUL looks like this
>
> <?xml version="1.0" encoding="UTF-8"?>
> <?xml-stylesheet href="/css/default_PC.css" type="text/css"?>
> <window width="602" height="452" style="background: #FFFFFF;"
> xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
> title="Screen2" UserEdit="yes" align="left">
> <stack>
> <button id="Button1" width="80" height="28" left="44" top="41"
> style="background: #5F6BA7;border-color: #6E7AB0;" label="My
> Button"></button>
> <button id="Button2" width="80" height="28" left="155" top="40"
> style="background: #5F6BA7;border-color: #6E7AB0;" label="My
> Button"></button>
> <button id="Button3" width="80" height="28" left="249" top="37"
> style="background: #5F6BA7;border-color: #6E7AB0;" label="My
> Button"></button>
> </stack>
> </window>
>
> I don't understand how to make the resolver undertand ?
Here is a working example, the prefix 'xul' is bound to the XUL
namespace so that in XPath expression you can use 'xul:xulelementname'
e.g. 'xul:button' to access XUL elements:
<?xml version="1.0" encoding="UTF-8"?>
<window width="602" height="452" style="background: #FFFFFF;"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
title="Screen2" align="left">
<script type="text/javascript">
<![CDATA[
function testXPathEvaluation () {
var resolver = document.createNSResolver(document.documentElement);
var xpathResult = document.evaluate(
'//xul:button | //xhtml:input',
document,
function lookupNamespaceURI (prefix) {
var nsURI = null;
switch (prefix) {
case 'xul':
nsURI =
'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
break;
default:
nsURI = resolver.lookupNamespaceURI(prefix);
break;
}
return nsURI;
},
XPathResult.ORDERED_NODE_ITERATOR_TYPE,
null
);
var result = '';
var node;
while ((node = xpathResult.iterateNext())) {
result += 'nodeType: ' + node.nodeType + '; nodeName: ' +
node.nodeName + '\r\n';
}
alert(result);
}
]]>
</script>
<stack>
<button id="Button1" width="80" height="28" left="44" top="41"
style="background: #5F6BA7;border-color: #6E7AB0;" label="My
Button"></button>
<button id="Button2" width="80" height="28" left="155" top="40"
style="background: #5F6BA7;border-color: #6E7AB0;" label="My
Button"></button>
<button id="Button3" width="80" height="28" left="249" top="37"
style="background: #5F6BA7;border-color: #6E7AB0;" label="My
Button"></button>
<xhtml:input type="button" value="html click button"
onclick="testXPathEvaluation();"/>
</stack>
</window>
As said the problem with XPath (XPath 1.0) is that any XPath expression
trying to access elements in the default namespace (e.g. <element
xmlns="http://example.com/whatever">) needs to use a prefix bound to
that namespace.
--
Martin Honnen
http://JavaScript.FAQTs.com/