🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators XHtmlReportAsString
This page was created by Lars.olofsson on 2020-06-18. Last edited by Wikiadmin on 2026-07-29.

You can use XHtmlReportAsString in OCL to merge an XHTML or XML template with values from a ViewModel and return the completed document as a string.

What it does

XHtmlReportAsString processes an XHTML string using a reporting ViewModel. Attribute values in the ViewModel replace matching template tags, and the operator returns the resulting XHTML as a string.

Use this operator when your template is available as text and you need the generated XHTML for later use. For the complete HtmlReport model setup, supported template locations, list rendering, and the Blob-based alternatives, see Documentation:HtmlReport.

Syntax

ResultHtmlAsString := self.XHtmlReportAsString(ReportRoot.ViewModels.ReportingViewmodel)
Part Meaning
self The root object of the reporting ViewModel.
ReportRoot The class that owns the reporting ViewModel.
ViewModels The method that retrieves the ViewModels of that class.
ReportingViewmodel The name of the ViewModel that supplies both the template location and the values to insert.
ResultHtmlAsString A string variable or expression result that receives the generated XHTML.

Replace attribute tags

Write a tag as the ViewModel attribute name enclosed in percent signs: %AttributeName%.

For example, assume the reporting ViewModel provides these values:

ViewModel attribute Value
Name Sandra
Age 30

Use the attributes in an XHTML template:

<html>
  <body>
    <p>Name: %Name%</p>
    <p>Age: %Age%</p>
  </body>
</html>

When you call XHtmlReportAsString, the result is:

<html>
  <body>
    <p>Name: Sandra</p>
    <p>Age: 30</p>
  </body>
</html>

The operator finds %Name% and replaces it with Sandra, finds %Age% and replaces it with 30, and returns the whole transformed XHTML document.

Prepare a valid template

HtmlReport processes the template in an XHTML/XML DOM. XHTML is an XML-compatible form of HTML and must have one root node. Put each replacement tag inside a valid element so that the DOM can detect it.

This template is valid for replacement:

<html>
  <p>%Name%</p>
</html>

A document containing only an <html> element is not enough to make a replacement tag detectable; the tag must be contained in a valid node such as <p>.

If the input is plain HTML without a <root>, <body>, or <html> element, XHtmlReportAsString attempts to add an <html> root element. For example:

<p>Hello %Name%</p>
<p>You need to do this....</p>

can produce:

<html>
  <p>Hello Sandra</p>
  <p>You need to do this....</p>
</html>

Provide a complete, single-root XHTML document when possible. This makes the template structure explicit and avoids DOM-loading issues.

Render a list from a multi-link

A multi-link in the ViewModel represents a list. To repeat the element that contains a list item, start that element with %%+MultiLinkName%. The containing element and its contents repeat once for each object in the multi-link.

For example, an Invoices multi-link with Date and Customername attributes can populate table rows:

<html>
  <body>
    <table>
      <tbody>
        <tr>
          <td>Date</td>
          <td>Customer</td>
        </tr>
        <tr>%%+Invoices%
          <td>%Date%</td>
          <td>%Customername%</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

The <tr> element repeats for every object in Invoices. Within each repeated row, %Date% and %Customername% resolve against the current invoice object.

Discover the available tags

Add %meta% as the first string in an element to have HtmlReport output the tags available from the ViewModel:

<html>
  <meta>%meta%</meta>
</html>

The output can contain a list such as:

<html>
  <meta>%TemplateHtml%, %TemplateBlob%, %ReportFileName%, %Name%, %Age%</meta>
</html>

Copy the tag name, including both percent signs, into the template. The %meta% value must be the first string in its element to be recognized.

Troubleshooting

Symptom Check
A tag is not replaced Confirm that the ViewModel exposes an attribute with the same name and that the template uses %AttributeName%, including percent signs.
No tags are detected Ensure that every tag is inside a valid XHTML/XML element, such as <p>%Name%</p>.
The template does not load as expected Use XHTML/XML with one root node. Do not rely on plain HTML generated by an editor unless you verify that the resulting document can be loaded into an XHTML/XML DOM.
A list does not repeat Confirm that the ViewModel relationship is a multi-link and that the repeated element begins with %%+MultiLinkName%.

Related output formats

XHtmlReportAsString is the string-based HtmlReport option. Documentation:HtmlReport also documents opendocumentreportasblob, which works with UTF-8 Base64 content, and opendocumentreportshow, which opens the generated HTML in the browser.

Use Documentation:OCLOperators ViewModelAsXml when you need to serialize ViewModel content as XML rather than merge it into a template. Use Documentation:OCLOperators ViewModelAsJSon when the required output is JSON.

See also