Solved

Bitwise and in XSL

Posted on 2001-06-06
2
809 Views
Last Modified: 2012-06-27
I have a data field that represents a bunch of checkboxes, which is stored as a series of bits in a number variable.

i.e.

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)
  Checkbox1.checked=true;

if (value && 2)
  Checkbox2.checked=true;

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

<xsl:variable name="boxchecked">
  <xsl:choose>
    <xsl:when test="$datavalue and @BitMask != 0">
      off
    </xsl:when>
    <xsl:otherwise>
      on
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

... 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?
0
Comment
Question by:anthony_glenwright
2 Comments
 
LVL 6

Accepted Solution

by:
wbradney earned 200 total points
ID: 6161047
anthony_glenwright,

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="http://www.w3.org/1999/XSL/Transform"
                   xmlns:java="http://xml.apache.org/xslt/java"
                   exclude-result-prefixes="java"
               version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
         <xsl:apply-templates/>
    </xsl:template>
    <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))"/>
         <test>
              <xsl:attribute name="att"><xsl:value-of select="$repl"/></xsl:attribute>
         </test>
    </xsl:template>
</xsl:stylesheet>

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).

Regards,
WMB
0
 
LVL 6

Author Comment

by:anthony_glenwright
ID: 6162235
That's more or less what I did, only I'm using the Microsoft parser, so I did:

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt"
   xmlns:javacode="http://www.inventua.com/code">
 
 
  <msxsl:script language="JScript" implements-prefix="javacode">
    function bitwiseand(first, second)
    {
      return (first &amp; second);
    }
  </msxsl:script>
 
and...

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

Thanks for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now