We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Parsing nested fields IF

I have a need to parse a nested IF statement to show it content from open tag to closing tag.

For example, i want to be able to parse the following:
{ IF {MERGEFIELD Additional ####,###.00} = “0” “” “with additional amount of ${MERGEFIELD Additional # ###,###.00}” }

When using other forum’s code (https://forum.aspose.com/t/56174), I only get part of it.
IF MERGEFIELD Additional # ###,###.00

Furthermore, I also need to know that the “MERGEFIELD Additional” was nested inside the “IF” field. So looking at the 1st paragraph in the example attached, I need to build the following structure:
Id Parent FieldText
==========================================================================================================================================================================
0 IF {MERGEFIELD Additional ####,###.00} = “0” “” “with additional amount of ${MERGEFIELD Additional # ###,###.00}” }
1 0 {MERGEFIELD Additional ####,###.00}
2 0 {MERGEFIELD Additional # ###,###.00}

I also need to work with other fields like SET (see 2nd paragraph in sample attached).


Hi Paul,


First of all, please note that DocumentExplorer is a very useful tool which easily enables us to see the entire document structure. You can find DocumentExplorer in the folder where you installed Aspose.Words e.g. C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Demos\CSharp\DocumentExplorer\bin\DocumentExplorer.exe. Below is the DOM structure of your document as viewed with DocumentExplorer:

>> I have a need to parse a nested IF statement to show it content from open tag to closing tag.

I am afraid, there is no direct way to capture nested IF statement as whole.

Hope this will help.

Hi there,


Thanks for your inquiry.

I apologise for the delay. The reason the code you have found isn’t working is because it does not take into account nested fields, therefore stops at the end of the merge field instead of at the end of the IF field.

Please try using the GetFieldCode method from the following post which can handle such situations: http://www.aspose.com/community/forums/263016/merging-of-conditional-if-with-autotext-fields-as-condition-within-if/showthread.aspx#263016

Regarding building a view of the field structure in a document, you can easily achieve this by using DocumentVisitor. Please see the documentation for details: http://docs.aspose.com/display/wordsnet/How+to++Extract+Content+using+DocumentVisitor

In this case you can override the VisitFieldStart and VisitFieldEnd methods and keep a count of the fields and there relations to one another.

If we can help with anything else, please feel free to ask.

Thanks,

http://www.aspose.com/community/forums/263016/merging-of-conditional-if-with-autotext-fields-as-condition-within-if/showthread.aspx#263016

The link above, referenced in the preceding post, is not working or broken.

I would like to know an approach to a similar, if not the same, issue.

I have fields in a document, and these fields can be nested within IF fields. I am trying to find out how to figure out the expressions on which they depend.

For example:

{ IF {MERGEFIELD MyField } = true “{MERGEFIELD YourField}” “{MERGEFIELD HisField}” }

Above, we can see that YourField is based on MyField = true, and also, that HisField is based on MyField <> true.

MyField, YourField and HisField are all tied to controls in the UI. Therefore, if we could develop the dependency relationships programatically with Aspose, we could enable the control for YourField if MyField = true. Otherwise, we could disable it.

It is a little more complex, but the same idea, for nested IF fields., like this:

{ IF {MERGEFIELD MyField } = true “{MERGEFIELD YourField}” "{ IF {MERGEFIELD HisField} = true “{MERGEFIELD HisDog}” “{MERGEFIELD HisCat” " }

Here, HisDog is based on (MyField <> true AND HisField = true).

HisCat is based on (MyField <> true AND HisField <> true).

Thus, knowing this programatically, we could enable the textBox for HisDog and disable it appropriately, based on the values provided in the UI to MyField and HisField.

I think this makes perfect sense, and I believe it is feasible. It’s just a matter of how much trouble one has to go through to build the expressions.

Thanks.

Hi Jeff,


Thanks for your inquiry. The forum link is not working for you because it is private for another user. In this forum link the GetFieldCode method was shared which is old approach. Please use the Field.GetFieldCode method to get the text between field start and field separator (or field end if there is no separator).
JeffMatthews:
I have fields in a document, and these fields can be nested within IF fields. I am trying to find out how to figure out the expressions on which they depend.
Please note that Aspose.Words tries to mimic the same behaviour as MS Word do. Aspose.Words do the mail merge and IF field evaluation in the same way as MS Word do. You can get the field result by using Field.Result property.

A field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. The Start, Separator and Endproperties point to the field start, separator and end nodes of the field respectively.

In your case, If the value of MyField is true, the IF files will be as following after doing mail merge.

{ IF true = true YourField_Result…

You can iterate through each IF field and get the field result by using Field.Result property. Hope this answers your query. Please let us know if you have any more queries.


Hi, Tahir. Your reply was close to what I am wanting to know.

Let me be more precise. Let’s say the template has this expression:

{ IF { MERGEFIELD MyField } = true “{ IF { MERGEFIELD HisField } = true “{ MERGEFIELD ThisField }” “{ MERGEFIELD ThatField }” }” “{ MERGEFIELD TheOtherField }” }

I want to look into the above with Aspose and generate the following:

For ThisField: " ( MyField == true && HisField == true ) "

For ThatField: ( MyField == true && HisField != true ) "

For TheOtherField: " MyField != true"

As you can see, I am wanting to build the compound condition strings associated with whether the target field (This, That or the Other) is in the result of ANY field in the merged template.

Here would be an example of needing to build an OR expression:

{ IF { MERGEFIELD MyField } = true “{ MERGEFIELD ThisField }” }
{ IF { MERGEFIELD YourField } = true “{ MERGEFIELD ThisField }” }

Here, you would want to build the OR expression for ThisField as

( MyField == true || YourField == true )

These expressions could be run in our code for run-time evaluation. Then, we could enable or disable controls in a manner which matches the Word template’s logic.

Let’s say MyField is really a Visual Studio C# checkBox. YourField is, too. Let’s finally say that ThisField is a textBox.

Basically, the ThisField textBox would be enabled only if MyField OR YourField was checked. This makes the User Interface form’s enable/disable logic match the needs of the Word template. Rather than have to duplicate the logic by expressly coding it in the UI, we ought to be able to create it from the template and apply it by using run-time evaluation on an equation.


Hi Jeff,


Thanks for sharing the detail. After the execution of mail merge operation the mail merge fields are replaces with values as shown below.

{ IF { MERGEFIELD MyField } = true "{ IF { MERGEFIELD HisField } = true …

Output : { IF true = true "{ IF ture = true…

Aspose.Words does not offer any interaction between UI and template document. However, you can get the field result by using Field.Result property. In your scenario, I suggest you please evaluate the complete expression (IF and Mail Merge) at UI end according to your data.

Hi Jeff,


Just a hint, you can now find the Field from an existing field in your document by using the FieldChar.GetField method. This should hopefully help with what you are trying to do.

If we can help with anything else, please feel free to ask.

Thanks,