When insert HTML string into document, an error: Cannot insert a node of this type at this location

When I insert an HTML string into the document, an error is prompted during the insertion: “Cannot insert a node of this type at this location.”
I am using version 25.12 ,aspose-words-25.12-jdk16

String htmlString = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mo>{</mo><mtable><mtr><mtd><mo>(</mo><mi>x</mi><mo>-</mo><mn>3</mn><msup><mrow><mo>)</mo></mrow><mrow><mn>2</mn></mrow></msup><mo>+</mo><msup><mrow><mi>y</mi></mrow><mrow><mn>2</mn></mrow></msup><mo>=</mo><mn>9</mn><mo>,</mo></mtd></mtr><mtr><mtd><mi>x</mi><mo>=</mo><mi mathvariant=\"italic\">my</mi><mo>+</mo><mn>2</mn><mo>,</mo></mtd></mtr></mtable></math>";
Document htmlDoc = new Document();
DocumentBuilder htmlDocbuilder = new DocumentBuilder(htmlDoc);
htmlDocbuilder.moveToDocumentEnd();	    	        
htmlDocbuilder.insertHtml(htmlString);
htmlDoc.save("D:/20251229.docx");

@hlgao

The error “Cannot insert a node of this type at this location” when using DocumentBuilder.insertHtml(htmlString) with MathML content in Aspose.Words for Java 25.12 is likely due to the following:

  1. MathML is not standard HTML: The insertHtml method expects valid HTML that maps to Word-compatible document nodes. Your input is MathML, an XML-based markup for mathematical expressions, which Aspose.Words does not directly support via insertHtml . Inserting raw MathML can result in node types that are incompatible with the document structure, especially when not wrapped in proper block-level HTML elements like <p> or <div> .

  2. Known regression in Aspose.Words 25.1+: There is a reported issue starting from version 25.1 (and present in 25.3) where insertHtml throws this error, which did not occur in version 24.10 or earlier . Since you’re using version 25.12, this regression may be contributing to the problem.

  3. Recommended workaround: Instead of inserting MathML via insertHtml, use Aspose.Words’ native OfficeMath class to build equations programmatically. This ensures compatibility and avoids HTML parsing issues . For example:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use builder.insertOfficeMath() or work with WMath objects

You may test with version 24.10 to verify if the issue is version-specific. If the problem persists with valid HTML input, it may indicate a bug in the HTML parser’s handling of XML namespaces or MathML-like structures .

@Professionalize.Discourse
Thank you very much. However, in our actual usage, HTML code and MathML markup are mixed together. If we use insertOfficeMath, it would require a large amount of code analysis and rewriting.
We tested this in version 24.12 and it works correctly.
Moreover, we have found that it is not that this kind of mixed usage is unsupported; rather, certain MathML tags cause the issue. For example, in this string, if the <mtable> tag pair is removed, the code runs normally.
In our case, what would be the recommended way to use it?

@hlgao
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28967

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as WORDSNET-28967) have been fixed in this Aspose.Words for Java 26.2 update.

Thank you very much, I’ll go try out the feature.

1 Like