Solved

Bitwise and in XSL

Posted on 2001-06-06
2
811 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XSD Data to SQL Tables 2 25
Xpath for list of names of first children nodes 5 51
Specific format 21 181
How to use xmlReader and idatareader  ? 4 55
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

14 Experts available now in Live!

Get 1:1 Help Now