Bitwise and in XSL

I have a data field that represents a bunch of checkboxes, which is stored as a series of bits in a number variable.


Checkbox1 = bit 1
Checkbox2 = bit 2

In XSL, I have code to create the checkboxes, but I am having trouble doing a bitwise 'and' operation.  In JavaScript, I would have code like:

if (value && 1)

if (value && 2)

(&& means 'and' in Javascript).  Similar code in XSL:

<xsl:variable name="boxchecked">
    <xsl:when test="$datavalue and @BitMask != 0">

... always returns 'true', because XSL seems to simply test for the existence of the variable or attribute, so instead of '$datavalue and @BitMask != 0' being calculated as (for example) '35 and 4', which should be zero, it calculates it as "true and true" because both the $datavalue variable and @BitMask attribute exist.

Is there any way to do a bitwise operation in XSL?
Who is Participating?
Wayne BradneyConnect With a Mentor Independent ConsultantCommented:

I don't think you can do bitwise operations directly in XSL, but if you're Java/Javascript there's a very cool way of performing arbitrary operations through extensions. You'd have a stylesheet like this (this is an example of a Java string replace, but you'll get the idea):

<xsl:stylesheet xmlns:xsl=""
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
    <xsl:template match="test[@att]">
         <xsl:variable name="str" select="@att"/>
         <xsl:variable name="find" select="_string_"/>
         <xsl:variable name="sub" select="_other_"/>
         <xsl:variable name="repl" select="java:Replacer.replace(string($str), string($find), string($sub))"/>
              <xsl:attribute name="att"><xsl:value-of select="$repl"/></xsl:attribute>

So you'd write a class Replacer with a method:

public static String replace(String, String, String)

that does whatever you like (in your case a class that does bitwise numeric comparisons).

anthony_glenwrightAuthor Commented:
That's more or less what I did, only I'm using the Microsoft parser, so I did:

<xsl:stylesheet version="1.0"
  <msxsl:script language="JScript" implements-prefix="javacode">
    function bitwiseand(first, second)
      return (first &amp; second);

  <xsl:when test="javacode:bitwiseand(number($datavalue), number(@BitMask)) != 0">
    <INPUT TYPE="checkbox" checked="" bitmask="{@BitMask}" onclick="storeCheckboxValue(this, '{$datafield}');" />  
    <INPUT TYPE="checkbox" bitmask="{@BitMask}" onclick="storeCheckboxValue(this, '{$datafield}');" />

Thanks for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.