Solved

Bitwise and in XSL

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to display the contents of an XML file using C# when associated elements are not found? 5 52
XML SQL 8 27
JSON  parse help 7 40
How to implement an additional command 16 40
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 …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

790 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