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
- Generate or obtain the XML string that you want to transform. For ViewModel output, see ViewModelAsXml.
- Write an XSLT stylesheet that copies, removes, or replaces the required XML nodes.
- Pass the stylesheet text as the first argument and the XML text as the second argument to
selfVM.XsltTransformXml. - Inspect the returned XML string and verify the required element names, attributes, and omitted nodes.
