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

You can use selfVM.XsltTransformXml(<xsltstring>, <xmlstring>) in OCL to transform an XML string with an XSLT stylesheet; it is intended for ViewModel-based XML output that must match another XML structure.

Syntax

selfVM.XsltTransformXml(<xsltstring>, <xmlstring>)
Argument Description
<xsltstring> A string that contains the XSLT transformation code.
<xmlstring> The XML data string to transform.
Result A new XML string produced by the XSLT transformation.

Transform ViewModel XML

Use this operator when the XML produced by ViewModelAsXml needs a different element structure or element names. Create the XML with the required ViewModel configuration first, then provide that XML as the second argument to XsltTransformXml.

For example, the following stylesheet:

  • copies attributes and nodes by default;
  • omits every <xslt> element from the result; and
  • replaces a root element named <root> with <PrspctsDataRpt>, while retaining the root element's attributes and child nodes.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*" />

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xslt"/>

  <xsl:template match="root">
    <PrspctsDataRpt>
      <xsl:apply-templates select="@*|node()" />
    </PrspctsDataRpt>
  </xsl:template>
</xsl:stylesheet>

Example input and result

Given this XML input:

<root reportDate="2026-07-28">
  <xslt>Do not include this element</xslt>
  <customer>
    <name>Contoso</name>
  </customer>
</root>

the stylesheet produces XML with the renamed root and without the <xslt> element:

<PrspctsDataRpt reportDate="2026-07-28">
  <customer>
    <name>Contoso</name>
  </customer>
</PrspctsDataRpt>

How the stylesheet works

Copy unchanged content

The template matching @* | node() is an identity template. It copies each attribute and node, then applies the templates again to its attributes and child nodes. This preserves XML that does not have a more specific template.

Remove an element

<xsl:template match="xslt"/>

This empty template matches an element named xslt and creates no replacement output. The matching element and its contents are therefore omitted.

Rename the root element

<xsl:template match="root">
  <PrspctsDataRpt>
    <xsl:apply-templates select="@*|node()" />
  </PrspctsDataRpt>
</xsl:template>

This template replaces the matched root element with PrspctsDataRpt. The select="@*|node()" expression processes the original element's attributes and child nodes, so they remain in the transformed result.

Build and test the transformation

  1. Generate or obtain the XML string that you want to transform. For ViewModel output, see ViewModelAsXml.
  2. Write an XSLT stylesheet that copies, removes, or replaces the required XML nodes.
  3. Pass the stylesheet text as the first argument and the XML text as the second argument to selfVM.XsltTransformXml.
  4. Inspect the returned XML string and verify the required element names, attributes, and omitted nodes.

See also