Computing a power of 2 in an expression box - InfoPath Dev
in

InfoPath Dev

Computing a power of 2 in an expression box

Last post 10-19-2007 01:22 AM by brisco. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 10-18-2007 02:12 PM

    • brisco
    • Not Ranked
    • Joined on 05-25-2007
    • Posts 6

    Computing a power of 2 in an expression box

    Hi,

    I've spent a good part of the afternoon trying to get an expression box to display a calculated value which uses a "non-trivial" (i.e. not available in XPath 1.0) function. In an element with 3 attributes A, B and C I'd like to display a D value computed as D = C / (2^(A-B)) but D is redundant with A, B and C and I don't want to have to add it to the schema. It's really for information purposes only... I'd like it to be updated whenever A, B or C change however !

    I did manage to display C / (A-B) using an expression box and the following Xpath: "@C div (number(@A) - number(@B))", but somehow I've not managed to compute 2^(A-B). I tried various tricks (including "xdExtension:computeD(A,B,C)" and computeD(...) implemented in script.js, or a secondary datasource with a table of powers of 2) without success :-( I must say that I'm new to JScript and such non-trivial use of Infopath, so I may have missed something pretty obvious.

    Note : A-B is an integer varying in a known range (1 to 23) which makes it possible to pre-compute the values if that can help.

     Any help would be appreciated :-) Thanks !

  • 10-18-2007 02:31 PM In reply to

    Re: Computing a power of 2 in an expression box

    Introduce a secondary data source with a repeating element which will contain all the values, something like that:

    <values>
      <value>2</value>
      <value>4</value>
      <value>8</value>
      ..
      <value>8388608</value>
    </values>

    Then in your expression box, refer to the row that corresponds to the value you want, something like this:

    xdXDocument:GetDOM("values")/values/value[position() = xdXDocument:get-DOM()/my:myFields/my:A - xdXDocument:get-DOM()/my:myFields/my:B]

    This expresssion will return the power of two corresponding to the difference between A and B (I am assuming A, B are children of myFields and the secondary data source is named "values")

    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 10-19-2007 01:22 AM In reply to

    • brisco
    • Not Ranked
    • Joined on 05-25-2007
    • Posts 6

    Re: Computing a power of 2 in an expression box

    Thanks for the tip, but it actually only "kind of" works... The problem is that my A, B and C attributes are in a repeating block, and I'd like the expression box to display the value for the current element.

    Imagine I have the following XML:

    <root> 
      <element A="15" B="12" C="1024"/> 
      <element A="14" B="10" C="103582"/>
    </root> 

    Assuming my formula is D = C / (2^(A-B)), I'd like to display (pardon the basic layouting !)

    15    12    1024     128.0

    14    10    103582    6473.875

     

    With the above instructions and the following Xpath:

    "@C div xdXDocument:GetDOM("Util")/PowersOfTwo/PowerOfTwo[position() = xdXDocument:get-DOM()/root/element/@A - xdXDocument:get-DOM()/root/element/@B] 

    the values in both expression boxes use the same A and B, but different C values. I somehow understand that as @C appears before we change contexts and start from the document root again. But that does not answer my needs :-(

     

    Note that I've removed the my: prefix which Infopath says is unknown. I don't think that explains the problem, though...

Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.