🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
XML
This page was created by Stephanie on 2023-08-04. Last edited by Wikiadmin on 2026-07-29.

You can use XML (eXtensible Markup Language) in MDriven to exchange structured data with external systems, import XML into model objects, generate XML from a ViewModel, transform XML, and validate XML against schemas.

What XML is

XML is a text-based markup format for representing structured data. An XML document uses elements, attributes, and a single document root element to describe that structure.

For example, this document has one root element and two person elements:

<?xml version="1.0"?>
<root>
  <person id="1">
    <name>Alan</name>
    <url>http://www.google.com</url>
  </person>
  <person id="2">
    <name>Louis</name>
    <url>http://www.yahoo.com</url>
  </person>
</root>

In this example:

  • root is the document root element.
  • Each person element is a repeating item.
  • id is an XML attribute.
  • name and url are child elements that hold values.

XML describes data rather than how to display it. HTML is also markup, but HTML primarily describes presentation in a browser. XML element names are defined by the data format or integration contract; they are not a predefined set of tags.

Use XML in MDriven

MDriven works with XML as text in OCL expressions and can map that text to model data or produce it from a ViewModel. Choose the operation based on what you need to do.

Goal MDriven feature Example
Import received XML into model objects XmlToObjects Turn repeating person elements into related objects.
Build a model structure from sample XML Using JSON or XML as class template Create a starting class structure from an XML example.
Generate XML for an external system ViewModelAsXml Export report data represented by a ViewModel.
Change one XML format into another XsltTransformXml Rename a root element with an XSLT transformation.
Check XML against one or more XML Schemas XmlValidateWithSchemas Validate an XML payload before it is sent.
Exchange a model with other modeling tools XMI Import or export the XML-based XMI 2.4.2 format.

Import XML into your model

Use selfVM.XmlToObjects when you receive XML and want MDriven to create objects for further processing. The operation converts the XML to JSON internally and then maps the resulting structure to classes, attributes, and associations.

For the sample XML above, you can use a root class such as ImportFile with an association that represents the root element. The object reached through that association can have a many association named person. The objects in that association can have name and url attributes.

The XML names that matter for this mapping are association and attribute names. Class names may be different. A single XML structure maps naturally to a single association, while repeated XML elements map naturally to a many association.

A typical action expression is:

vNewImportFile:=selfVM.XmlToObjects(ImportFile, self.XmlInput)

This expression uses XML text from self.XmlInput, creates an ImportFile root object, and assigns that new object to vNewImportFile.

Import mapping behavior

  • XML content for which no matching attribute or association exists in your classes is ignored.
  • Your classes may contain additional attributes or associations that are not present in the XML.
  • An optional RawJSon : String attribute on the target class receives the JSON handled during the import. This can help you inspect the converted structure while developing the mapping.

For a complete worked example, including ViewModel input and a Prototyper result, see Documentation:Import xml and JSon with MDriven.

Generate XML from a ViewModel

Use selfVM.ViewModelAsXml to create XML text from a ViewModel. This is appropriate when you need an XML export, report payload, or download generated from data already presented through a ViewModel.

For example:

selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView)

The result is XML text. By default, the generated representation uses a root element. You can set the XmlRootTag tagged value on the ViewModel when the receiving system requires another root element name.

Control the XML shape

The following tagged values on ViewModel attributes and associations control how ViewModelAsXml renders the XML.

Tagged value Effect Example use
NodeName Uses this value instead of the ViewModel attribute name as the XML node name. Output an element name containing a dash.
XmlChildNode Changes whether an association is rendered with a wrapping child element. For a single association, true renders it as a child element. For a many association, false omits the default outer child element. Match the nesting required by an external XML format.
XmlAttribute Renders a ViewModel attribute as an XML attribute instead of an XML element. Produce <TimeCode Code="11061" />.
XmlParentValue Renders an attribute value as the value of its parent element rather than as its own element. Produce <Status>Approved</Status> from a parent value.

Do not use XmlParentValue on more than one attribute of the same ViewModel class; the result is undefined. Also, NodeName does not add a namespace to an element name. If a node needs a namespace different from its parent, add an xmlns attribute on that node.

Prepare generated XML for download

If the receiving system requires an XML declaration rather than the default root wrapper, you can replace the wrapper and add a declaration:

vCurrent_ReportExportView.XML := selfVM.ViewModelAsXml('ReportExportToXML', vCurrent_ReportExportView).
replace('<root>', '<?xml version="1.0" encoding="ISO-8859-1"?>').
replace('</root>', '')

The .NET string type is Unicode in memory. If the declaration specifies ISO-8859-1, encode the text accordingly before creating a Blob for download:

self.XML.StringToEncodedBase64(28591).Base64ToBlob

Here, 28591 is ISO-8859-1 Latin 1. Creating a Blob is required for a download link. See Documentation:OCLOperators ViewModelAsXml for the complete export guidance.

Transform XML with XSLT

Use selfVM.XsltTransformXml when you have XML in one valid structure but need another structure. The first argument is XSLT text and the second argument is the XML text to transform. The result is a new XML string.

For example, you can use an XSLT template to replace a root element with PrspctsDataRpt while retaining its content. Keep the XSLT separately understandable and test the resulting XML against the contract required by the recipient.

Validate XML before sending or processing

Use selfVM.XmlValidateWithSchemas to validate XML against one or more XML Schema URLs. The operation returns strings that can include validation errors and warnings.

For example:

selfVM.XmlValidateWithSchemas(
  '<root/>',
  Set{'http://www.gdsregistry.org/3.1/schemas/gs1/gdsn/CatalogueItemNotification.xsd'}
)

Treat returned errors as data-quality or contract failures that must be addressed before sending the payload. A warning such as No schema for root element indicates that the supplied schemas do not define the document root.

XML in report templates

HtmlReport also supports XML-style template documents. XML requires a single document root node. When you design a template, ensure that the output has one enclosing element and that element and attribute values follow the XML structure required by the target format.

See also