Skip to main content
Previous section   Next section

Defining Data Transformations for XML Virtual Documents

This chapter discusses how to create data transformations (specifically DTL-based transformations) for XML virtual documents, for use in rule sets. It discusses the following topics:

Creating a Data Transformation

To create a data transformation for XML virtual documents:

  1. Optionally load the applicable XML schema or schemas into Ensemble.

    See “Loading XML Schemas into Ensemble,” earlier in this book.

  2. Use the DTL editor in the Management Portal or in Studio, as described in Developing DTL Transformations.

  3. Within the data transformation, use the following values:

    • For Source Class and Target Class, use EnsLib.EDI.XML.Document, the class with which Ensemble represents XML virtual documents.

    • For Source Document Type, optionally select the XML type expected in the message. Choose an XML type from one of the XML schemas you have loaded into Ensemble.

      Leave this value blank if you do not have or do not want to use the schema.

    • For Target Document Type, optionally select a different XML type or remove the value.

      Ensemble initializes Target Document Type with the value you select for Source Document Type, if any.

  4. Create actions within the data transformation as usual, using the XML property paths described in the previous chapter. There are two basic scenarios:

    • If you have loaded the schemas and have specified the source and target document types, the DTL editor displays each document structure as a tree. Then you can drag and drop to create the transformation. Ensemble creates actions that use schema-dependent paths. You can edit these to use DOM-style paths instead, if those are needed for some reason.

    • If you do not specify the document types, the document structures are not displayed as trees. In this case, it is necessary to add and edit the actions manually. You can use only DOM-style paths.

    In either case, you can add code elements to support more complex processing.

After you save and compile the data transformation, it is available for use in a rule set; see the chapter “Defining Rule Sets for XML Virtual Documents.”

Available Assignment Actions for XML Virtual Documents

For XML virtual documents, Ensemble supports the following assignment actions:

  • set — Sets a value. If the type of the target element is "any", then the text can include XML markup. The XML markup must be well formed, but it will not be validated against any schema.

  • append — Appends the new value to the target element, after any subnodes in that element.

  • clear — Clears the text context of the target but retains the element and any children. Or, if the target is an attribute, the action clears its value but retains the attribute.

  • remove — Removes the target element or attribute.

Note that insert is not supported.

Using Code

If you need to add code elements to support more complex processing, you directly invoke the GetValueAt() and SetValueAt() methods of the source and target variables. For EnsLib.EDI.XML.Document, these methods are as follows:

GetValueAt()
method GetValueAt(pPropertyPath As %String, 
                  pFormat As %String, 
                  Output pStatus As %Status) as %String
Copy code to clipboard

Where:

  • pPropertyPath is an XML property path, as described earlier in this book.

  • pFormat is a set of flags that control the format of the returned string. See the following subsection.

  • pStatus is a status that indicates success or failure.

This method returns the current value at the given property path, or returns an empty string if the path is not valid.

SetValueAt()
method SetValueAt(pValue As %String, 
                  pPropertyPath As %String, 
                  pAction As %String = "set", 
                  pKey As %String = "") as %Status
Copy code to clipboard

Where:

  • pValue is a suitable value for the given XML property path.

  • pPropertyPath is an XML property path, as described in the previous chapter.

  • pAction is either "set", "append", "clear", or "remove". For details, see the previous section.

  • pKey is not used for XML virtual documents.

This method evaluates the given property path, and (if the path is valid), uses pValue and pAction to modify the value at that path.

Important:

It is useful to check the status values returned by these methods. The status contains specific information when you specify invalid paths or attempt actions that are not permitted. This information is particularly useful when you are debugging and can save you time.

The pFormat Argument

The pFormat argument for GetValueAt() is an optional string that controls the format of the returned string. This string can contain any suitable combination of the characters in the following table:

General Description Character to Include in Format Setting Specific Behavior
Line feeds and carriage returns w Adds a Windows-style carriage return and line feed combination after every text-free element.
r Uses the stored line feeds and carriage returns. This option takes precedence over the options w and n.
n Includes a new line (line feed) after every text-free element. In contrast to w, this option does not add a carriage return.
Indentation. Note that these options are used only if the output includes new lines. i Indents each new line with four spaces.
Any integer from 1 to 9 Indents each new line with this number of spaces. This option takes precedence over the previous indentation option.
t Indents each new line with a tab. This option takes precedence over both of the previous indentation options.
s Uses the stored indentation whitespace. This option takes precedence over the previous indentation options.
Handling attributes a Alphabetizes the attributes in an element.
q Uses double quotes (rather than single quotes) to set off attribute values if possible.
Handling namespaces p Suppresses output of namespace prefixes.
x Suppresses output of namespace declarations.
Handling empty elements e Generates output for each empty element with an open tag and close tag pair. If this option is not set, empty elements are output as a single empty tag.
Other c Canonical output. This option takes precedence over the options e i n t w
f Generates the full element (including both the starting and ending tags), not just the contents within the element.
l Includes information about the location of the schema file that was loaded into Ensemble. This option takes effect only if you use f.
o Includes any XML entities as is, rather than performing XML escaping for those entities.
C(e) Generates an XML header line that declares the given character encoding; e is the non-quoted name of a character encoding such as UTF–8. If e is empty, use the encoding defined by the adapter. If e begins with ! then force the encoding of the output stream. Note that this will be applied automatically for file operations configured with a non-UTF-8 character set.

As noted above, the pFormat argument can equal a combination of these items. For example, if you use the value C(UTF-8)q, the outbound document is in the UTF-8 character set and attributes are set off with double quotes. For another example, if you use the value C(UTF-16)a, the outbound document is in the UTF-16 character set and attributes are alphabetized.

Note:

This information also applies to the Format setting of an XML business operation.

Example 1: Copying Most of the Source Document

To easily define a data transformation that copies most of a source document, do the following in the Data Transformation Builder:

  • On the Transform tab, select copy from the Create drop-down list.

    Then, by default, the new document will be a copy of the original document.

  • Define actions that partly or fully remove selected elements or attributes. To define such an action:

    1. In Add Action, click clear or remove.

    2. Double-click the target property that you want to clear or remove.

    3. Enter any value into Value; this field is required but is ignored in this case.

The following shows an example that uses schema-dependent paths:

Class Demo02.MyDTL Extends Ens.DataTransformDTL
{

XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='EnsLib.EDI.XML.Document' targetClass='EnsLib.EDI.XML.Document' 
sourceDocType='Demo02:Patient' targetDocType='Demo02:Patient' create='copy' language='objectscript' >
<assign value='"this value is ignored"' property='target.{WorkAddress}' action='remove' />
<assign value='"this value is ignored"' property='target.{HomeAddress}' action='remove' />
</transform>
}

Parameter REPORTERRORS = 1;

}
Copy code to clipboard

This data transformation copies the source document to the target and then removes the <WorkAddress> and <HomeAddress> elements from the target.

The following shows an equivalent example that uses DOM-style property paths:

Class Demo02A.MyDTL Extends Ens.DataTransformDTL
{

XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='EnsLib.EDI.XML.Document' targetClass='EnsLib.EDI.XML.Document' 
create='copy' language='objectscript' >
<assign value='"this value is ignored"' property='target.{/Patient/WorkAddress}' action='remove' />
<assign value='"this value is ignored"' property='target.{/Patient/HomeAddress}' action='remove' />
</transform>
}

Parameter REPORTERRORS = 1;

}
Copy code to clipboard

Notice that in this case, the data transformation does not specify the document types because they are unnecessary here.

Example 2: Using Only a Few Parts of the Source Document

To easily define a data transformation that uses only a few parts of a source document, do the following in the Data Transformation Builder:

  • On the Transform tab, select new from the Create drop-down list.

    Then, by default, the new document will be empty.

  • Define actions that copy selected elements or attributes. To define such an action, drag and drop from the source document area to the target document area. Each action that you add this way is a set action.

The following shows an example that uses schema-dependent paths:

Class Demo05.MyDTL Extends Ens.DataTransformDTL 
{

XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='EnsLib.EDI.XML.Document' targetClass='EnsLib.EDI.XML.Document' 
sourceDocType='Demo05:Patient' targetDocType='Demo05:Patient' create='new' language='objectscript' >
<assign value='source.{MRN}' property='target.{MRN}' action='set' />
<assign value='source.{PrimaryCarePhysician}' property='target.{PrimaryCarePhysician}' action='set' />
</transform>
}

Parameter REPORTERRORS = 1;

}
Copy code to clipboard

This data transformation copies only the MRN and PrimaryCarePhysician properties from the source to the target.

The following shows an equivalent example that uses DOM-style property paths:

Class Demo05A.MyDTL Extends Ens.DataTransformDTL 
{

XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='EnsLib.EDI.XML.Document' targetClass='EnsLib.EDI.XML.Document' 
create='new' language='objectscript' >
<assign value='source.{/Patient/MRN}' property='target.{/Patient/MRN}' action='set' />
<assign value='source.{/Patient/PrimaryCarePhysician}' 
property='target.{/Patient/PrimaryCarePhysician}' action='set' />
</transform>
}

Parameter REPORTERRORS = 1;

}
Copy code to clipboard

Example 3: Using Code and SetValueAt()

The following example uses the code action type and uses a DOM-style path. It adds an attribute and an XML comment to the root element:

Class Demo06.MyDTL Extends Ens.DataTransformDTL 
{

XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='EnsLib.EDI.XML.Document' targetClass='EnsLib.EDI.XML.Document' 
create='copy' language='objectscript' >
<code>
<![CDATA[
 //this part adds an attribute to the document
 set path="/1/@NewAttribute"
 set status=target.SetValueAt("New attribute value",path)
 if 'status {do ##class(MyApp.Utils).Trace("Demo06.MyDTL","Error setting path: ",path)}

 //this part adds a comment to the document
 set path="/1/comment()"
 set status=target.SetValueAt("This is an XML comment",path)
 if 'status {do ##class(MyApp.Utils).Trace("Demo06.MyDTL","Error setting path: ",path)}
 ]]>
</code>
</transform>
}

Parameter REPORTERRORS = 1;

}
Copy code to clipboard

If the SetValueAt() method returns an error, this transformation uses a utility method to record the details.