?
Solved

Bitwise and in XSL

Posted on 2001-06-06
2
Medium Priority
?
820 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
Wayne Bradney earned 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

801 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