How to validate the doc with aspose tags

Hello Sir/Ma’am

I have one file looks like **example.zip (8.6 KB) ** attached with this message. And generate the document by providing values to the tags. Now we want to validate the document has valid aspose tags and syntax through our code.
Now would you please tell me how to validate that all the tags are correctly formatted and how to handle the errors in the document. As of now we manually check the whole document and all the tags to check the error.
Do you have any suggestions? We have aspose.words licence as well.

Note: The document attached has errors that i know but can you find without manually checking each tags and syntax of aspose tag? Any code to handle the errors?

Thank you.

@Iamaditya,

The following code can be used to determine:

  • The total number of paragraphs in document
  • How many valid tags are there and
  • It also highlights the valid tags

Hope, this helps.

Document doc = new Document("D:\\Example\\example.docx");
ReplaceEvaluatorFindAndHighlight handler = new ReplaceEvaluatorFindAndHighlight();

FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = handler;
options.Direction = FindReplaceDirection.Backward;

// We want the "your document" phrase to be highlighted.
Regex regex = new Regex(@"<<\[example_\d+\]>>", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, "", options);

int totalParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).Count;
int matches = handler.count;

Console.WriteLine("Total Paragraphs = " + totalParagraphs);
Console.WriteLine("Perfect matches = " + matches);

doc.Save("D:\\example\\18.8.docx");
///////////////////////////////////////////////
private static Run SplitRun(Run run, int position)
{
    Run afterRun = (Run)run.Clone(true);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring((0), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}


private class ReplaceEvaluatorFindAndHighlight : IReplacingCallback
{
    public int count = 0;

    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// This method highlights the match string, even if it spans multiple runs.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match, 
        // In this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further highlighting.
        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node. 
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.NextSibling;
            }
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {
            SplitRun((Run)currentNode, remainingLength);
            runs.Add(currentNode);
        }

        // Now highlight all runs in the sequence.
        foreach (Run run in runs)
            run.Font.HighlightColor = Color.Yellow;

        count++;

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;
    }
}

@awais.hafeez
Thank you for your kind reply. But we generate a document with this tags and provide the value. The value of the tags we get from the database. Now i want to validate that my document is generated and no error arised. How i can check that?

@Iamaditya,

Unfortunately, your question is not clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your usecase. Also, please share screenshot(s) that will help us to identify the invalid tags.

I know @awais.hafeez sir,

Thank you for letting me know. I will come up with a better clarification.

Hello Sir,
I am here again. The solution you gave above is not feasible for us. When we highlight the valid aspose tags, we again need to verify the tags in the document. This is a manual process. Is there any another method by which we can log all the invalid tags with line number?

@Iamaditya,

Please try the following code. Hope, this helps.

Document doc = new Document("D:\\Example\\example.docx");

NodeCollection paras = doc.GetChildNodes(NodeType.Paragraph, true);
for (int i = 0; i < paras.Count; i++)
{
    Paragraph para = (Paragraph) paras[i];
    string text = para.ToString(SaveFormat.Text);
    Regex regex = new Regex(@"<<\[example_\d+\]>>", RegexOptions.IgnoreCase);
    if (regex.IsMatch(text))
    {

    }
    else
    {
        Console.WriteLine($"Para {i} --> {text.Trim()} is InValid");
        Console.WriteLine("--------------------------------");
    }
}

@awais.hafeez
Thank you sir… It was helpful.
Now what about the conditional statement. How to verify them?
What approach you suggest?
For eg: If i have statement like
<<if[Condtion]>>
Statements…

<</ if>>
<<if[condition.Any(x=> x==“Something”)]>>
statements…
,
<</ if>>

@Iamaditya,

The operations, that you can perform by using MS Word, can also be performed by using Aspose.Words programmatically. You just need to come up with your own logic depending upon your scenarios. Please refer to the Aspose.Words documentation to learn about different features. Aspose.Words also offers a LINQ reporting engine that you may want to incorporate in your application.

@awais.hafeez
Thank you sir… It was helpful.
Now what about the conditional statement. How to verify them?
What approach you suggest?
For eg: If i have statement like
<<if[Condtion]>>
Statements…

<</ if>>
<<if[condition.Any(x=> x==“Something”)]>>
statements…
,
<</ if>>

I wanted to again ask this question. I checked the documentation but still not able to validate the if conditions.

@Iamaditya,

We have logged your requirement in our issue tracking system. The ID of your ticket is WORDSNET-17501. We will further look into the details of this problem and will keep you posted on any further updates. We apologize for any inconvenience.

@Iamaditya,

The only way to properly validate template syntax is trying to build a report, because we need actual data sources to answer the question whether template syntax is guaranteedly valid.

We cannot validate template syntax separately without actual data sources being present, because we cannot tell whether, for example, tag “<<if [obj.SomeValue]>>” is valid until we know whether “obj” has a field “SomeValue” or not.

For now, the only way to show a template syntax error for the engine is to throw an exception. However, we have plans to improve this by introducing an option to inline (and highlight) template syntax error messages in templates, exactly in places where the errors are encountered, which should simplify the process of template syntax errors finding and fixing. But there is no ETA for this feature as the corresponding issue (WORDSNET-15203) is postponed.

Thanks for the detailed answer.

Sir i don’t want to check whether obj has some value or not but i want to validate the static thing. You are correct we can’t show whether the obj is valid or not but we can show that whether the if is valid or not.

  1. If is in the correct format e.g: tag.Contains(<<if[Anything_Doesn’t_Matter]>>)
  2. Whether if is ended correctly or not. e.g: <</if>>
  3. And we have same number of end if as start if. <<if[]>> <<//if>> pair

@Iamaditya,

We have logged these details in our issue tracking system and will keep you posted on further updates.

A post was split to a new topic: Tool to Validate LINQ Reporting Template Syntax in Word Document | Aspose.Words for .NET