Shrout1
Shrout1

Reputation: 2607

Always return element value from current node with XPath

Edit: This question should read "How do I point to an element in the context node?" Answer is quite simple in this situation. From Larsh's response: "my:Line_Item_Mod_0 = 'true'"

Also, forgive me if this was a highly obvious question; I didn't know the terminology for "Context Node" which is probably why google and the StackOverflow search weren't helping much.

Read the remainder of the question for context.

I need an XPath expression that returns the current Line_Item_Mod_0 value within each node in order to conditionally format an MS InfoPath form. I'm quite green to XPath and I don't know what the appropriate term for this behavior (exhibited by an expression) is. Here is my XML:

<?xml version="1.0" encoding="UTF-8"?><?mso-infoPathSolution solutionVersion="1.0.0.504" productVersion="14.0.0" PIVersion="1.0.0.0" href="file:///C:\Documents%20and%20Settings\Chris\Local%20Settings\Application%20Data\Microsoft\InfoPath\Designer3\c37482678ae440c9\manifest.xsf" ?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?><?mso-infoPath-file-attachment-present?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-05-05T19:56:08" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:MasterSection>
    <my:Project_Information>
        <my:Line_Items>
            <my:Counter>1</my:Counter>
            <my:Authorized_Number>4</my:Authorized_Number>
            <my:XPathLINCounter>Line Item 1</my:XPathLINCounter>
            <my:Line_Item_Mod_0>false</my:Line_Item_Mod_0>
        </my:Line_Items>
        <my:Line_Items>
            <my:Counter>2</my:Counter>
            <my:Authorized_Number>7</my:Authorized_Number>
            <my:XPathLINCounter>Line Item 2</my:XPathLINCounter>
            <my:Line_Item_Mod_0>true</my:Line_Item_Mod_0>
        </my:Line_Items>
        <my:Line_Items>
            <my:Counter>3</my:Counter>
            <my:Authorized_Number>2</my:Authorized_Number>
            <my:XPathLINCounter>Line Item 3</my:XPathLINCounter>
            <my:Line_Item_Mod_0>false</my:Line_Item_Mod_0>
        </my:Line_Items>
    </my:Project_Information>
</my:MasterSection>
</my:myFields>

I am trying to evaluate whether the "mod" value for the current node is 1 or 0. I already have an expression that does this, but for some reason InfoPath conditional formatting doesn't like it. You can see the results of this mod expression in the Line_Item_Mod_0 element.

All I need to do is target the Line_Item_Mod_0 element within each Line_Items node. I can't seem to make an expression that returns the value of the Line_Item_Mod_0 for its parent node (or perhaps "current" is the correct term).

I.E. Line_Items node 1 would have an expression within it that ALWAYS returns the false value of Line_Item_Mod_0.

I realize this should be simple, but I can't seem to get my expression to stick within its current node.

The expression (count(preceding-sibling::my:*) + 1) mod 2 > 0 generates the Line_Item_Mod_0 node but doesn't play nice with conditional formatting if I apply it as a rule. I can't tell why.

I want to try to bypass it and evaluate the "true" or "false" value directly.

Upvotes: 0

Views: 698

Answers (1)

LarsH
LarsH

Reputation: 28004

Whenever the current node (context node) is a <my:Line_Items> element, the following XPath expression will return the value of its <my:Line_Item_Mod_0> child element:

"my:Line_Item_Mod_0"

This XPath expression will actually select an element node; if that element node is evaluated in a string context, its value will be the string content of its child text node, false or true. If you need to convert that to a boolean, use

"my:Line_Item_Mod_0 = 'true'"

Note however, that this XPath expression uses a namespace prefix. In order for that usage to be valid, you have to declare that namespace prefix in the context of the XPath processor. You haven't shown any of the context of how you are invoking XPath, but if you do show us that, we should be able to help verify that that part is done correctly.

Also you mention "conditional formatting" a couple of times, but with insufficient detail to do anything with. I'm treating it as unimportant for now, but if it's important, please edit your answer to post examples so we can help you with it.

Upvotes: 1

Related Questions