Search code examples
xmlxpathinfopath

Return initial value or most recent occurance with XPath then run operator


I have the following XPath 1.0 expression in MS InfoPath (I will post accompanying XML):

//my:Value_Modifier_Group[1=count(following-sibling::my:Value_Modifier_Group)]//my:Modified_Start_Value[(text())] | //my:Start_Value[text() and not(//my:Value_Modifier_Group[1=count(following-sibling::my:Value_Modifier_Group)]//my:Modified_Start_Value[(text()])]

If there is an initial value and no "Modified Value" yet then the initial value is returned. If a modified value is present then it is returned.

I need to return that value and then perform the following operations on it:

number(//my:Value_Modifier_Group[1=count(following-sibling::my:Value_Modifier_Group)]//my:Modfied_Start_Value) + number(//my:Value_Modifier_Group[0=count(following-sibling::my:Value_Modifier_Group)]//my:Start_Value_Increase) - number(//my:Value_Modifier_Group[0=count(following-sibling::my:Value_Modifier_Group)]//my:Start_Value_Decrease)

This operation adds or subtracts an increase/decrease value. The problem is that this expression returns a NaN if no Modified_Start_Value is present. Works great otherwise.

I need to team both of these expressions together into a single XPath expression.

  • If Modified_Start_Value has a preceding value, use that in operations
  • If Modified_Start_Value does not have a preceding value then use Start_Value in operations instead
  • It is important that I always use the "preceding" value because the preceding value is fixed. It acts as a reference point or starting point.

Here is a sample of the XML I am using:

<?xml version="1.0" encoding="UTF-8"?><?mso-infoPathSolution solutionVersion="1.0.0.2" productVersion="14.0.0" PIVersion="1.0.0.0" href="file:///C:\Documents%20and%20Settings\Chris\Local%20Settings\Application%20Data\Microsoft\InfoPath\Designer3\ef41a19775b4ce0\manifest.xsf" ?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-09-19T13:28:35" xml:lang="en-us">
<my:Start_Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">6</my:Start_Value>
<my:Main_Group>
    <my:Value_Modifier_Group>
        <my:Modified_Start_Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">6</my:Modified_Start_Value>
        <my:Start_Value_Increase>0</my:Start_Value_Increase>
        <my:Start_Value_Decrease>0</my:Start_Value_Decrease>
    </my:Value_Modifier_Group>
<my:Value_Modifier_Group>
    <my:Modified_Start_Value>6</my:Modified_Start_Value>
    <my:Start_Value_Increase>0</my:Start_Value_Increase>
    <my:Start_Value_Decrease>0</my:Start_Value_Decrease>
</my:Value_Modifier_Group></my:Main_Group>
</my:myFields>

I hope this question isn't too hastily put together! Busy day today...


Solution

  • Ok, I am going to self answer this question as I just figured it out.

    The key is to combine the XPath expressions by placing the value from the first expression into a node and then referencing that node in the second expression.

    It seems that the pipe command | does not play well with other operators.

    The following expression returns values in the following way:

    • //my:Value_Modifier_Group[1=count(following-sibling::my:Value_Modifier_Group)]//my:Modified_Start_Value[(text())] | //my:Start_Value[text() and not(//my:Value_Modifier_Group[1=count(following-sibling::my:Value_Modifier_Group)]//my:Modified_Start_Value[(text()])]
    • If no preceding Modified_Start_Value is available before the first Modified_Start_Value node the expressions defaults to the initial value.
    • In this way you are guaranteed the most recent valid Start_Value or Modified_Start_Value

    This value of this expression is then put into a node titled StartVModStart. The XPath for this node is then put into the evaluating expression:

    ../../../my:StartVModStart + number(/descendant-or-self::node()/my:Value_Modifier_Group[0 = count(following-sibling::my:Value_Modifier_Group)]//my:Start_Value_Increase) - number(/descendant-or-self::node()/my:Value_Modifier_Group[0 = count(following-sibling::my:Value_Modifier_Group)]//my:Start_Value_Decrease)

    • This expression uses "the most recent" value and then adds or subtracts the changes

    In this way one can keep a "running sum" through the course of document, starting in an initial node and moving all the way through repeating nodes.

    This question seems highly convoluted, so forgive me if both my question and answer do not seem clear. As I reach for more complex functionality the required XPath expressions become more and more complex.