Discussion:
Using bitwise comparison in an XPath expression
(too old to reply)
Craig D.
2004-11-10 05:32:02 UTC
Permalink
I need to process a large number of combinations of nodes, testing whether or
not a certain bit is set in a number of key positions. It is possible,
though not desirable to div and mod to strip away the superfluous bits......

I have tried using the following (assuing x is a variable which contains the
flag.....)

<xsl:if test="($x and 1) = 1">
<!-- we know that bit 0 is set.......
</xsl:test>

This doesn't seem to return the answer I was expecting. For example, if the
flag has a value of 3, this means that bit 0 and 1 are both on....

I have written a small javascript function which works fine when using xslt,

function flag(a, b) {
return((a & (1<<b)) == (1<<b));
}

This correctly returns true or false depending on whether the bit
(zero-based) is set.

In my situation, however, I am dynamically selecting nodes using another
javascript function

function selectNodes(doc, xpath) {
return (doc[0].selectNodes(xpath));
}

This leaves me with the option to either:
(a) find a pure xpath equivalent to execute the bitwise comparison; or
(b) find a way to call an XSLT extension function from the selectNodes
method of the IXMLDOMNode object listed above (doc[0]).

Any help would be most appreciated.

Regards
Hans-Georg Michna
2004-11-10 08:52:11 UTC
Permalink
On Tue, 9 Nov 2004 21:32:02 -0800, "Craig D."
(a) find a pure xpath equivalent to execute the bitwise comparison ...
Craig,

that's certainly possible using integer arithmetic.

Hans-Georg
--
No mail, please.
Oleg Tkachenko [MVP]
2004-11-10 09:27:47 UTC
Permalink
Post by Craig D.
I need to process a large number of combinations of nodes, testing whether or
not a certain bit is set in a number of key positions. It is possible,
though not desirable to div and mod to strip away the superfluous bits......
XPath doesn't support bitwise operators. Usual advice is to go for
extension functions or emulate bitwise math using floor()/div/mod/etc
operators. E.g. a mod 2 gives you least significant bit, and
floor(a div 2) mod 2 gives you the next one etc.
Post by Craig D.
In my situation, however, I am dynamically selecting nodes using another
javascript function
function selectNodes(doc, xpath) {
return (doc[0].selectNodes(xpath));
}
(a) find a pure xpath equivalent to execute the bitwise comparison; or
(b) find a way to call an XSLT extension function from the selectNodes
method of the IXMLDOMNode object listed above (doc[0]).
It's not clear why do you need (b).
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Dimitre Novatchev
2004-11-12 20:39:53 UTC
Permalink
http://www.topxml.com/code/default.asp?p=3&id=v20010613074616


Cheers,
Dimitre.
Post by Craig D.
I need to process a large number of combinations of nodes, testing whether or
not a certain bit is set in a number of key positions. It is possible,
though not desirable to div and mod to strip away the superfluous bits......
I have tried using the following (assuing x is a variable which contains the
flag.....)
<xsl:if test="($x and 1) = 1">
<!-- we know that bit 0 is set.......
</xsl:test>
This doesn't seem to return the answer I was expecting. For example, if the
flag has a value of 3, this means that bit 0 and 1 are both on....
I have written a small javascript function which works fine when using xslt,
function flag(a, b) {
return((a & (1<<b)) == (1<<b));
}
This correctly returns true or false depending on whether the bit
(zero-based) is set.
In my situation, however, I am dynamically selecting nodes using another
javascript function
function selectNodes(doc, xpath) {
return (doc[0].selectNodes(xpath));
}
(a) find a pure xpath equivalent to execute the bitwise comparison; or
(b) find a way to call an XSLT extension function from the selectNodes
method of the IXMLDOMNode object listed above (doc[0]).
Any help would be most appreciated.
Regards
Loading...