Routing XML Virtual Documents in Productions
Specifying Schema-dependent Paths for XML Virtual Documents
|
|
This chapter describes how to specify schema-dependent paths for XML virtual documents. It discusses the following:
You can use these paths to access values and to set values.
To access the contents of an element, you can use one of the following schema-dependent paths. You also use these paths when you create more complex schema-dependent paths as discussed in later subsections.
Consider the following XML document:
<?xml version="1.0" ?>
<Patient MRN='000111222' xmlns='http://myapp.com'>
<Name>Georgina Hampton</Name>
<FavoriteColors>
<FavoriteColor>Red</FavoriteColor>
<FavoriteColor>Green</FavoriteColor>
</FavoriteColors>
<Address>
<Street>86 Bateson Way</Street>
<City>Fall River</City>
</Address>
<Doctor>
<Name>Dr. Randolph</Name>
</Doctor>
</Patient>
The following table shows some example paths for this document:
To access the value of an attribute, you can use one of the following schema-dependent paths. Here (and in the rest of this section),
element_reference is a complete schema-dependent path as described in the previous table.
The following table shows an example path for the previous document:
InterSystems IRIS™ removes the comments when it reads XML files. Consequently, you should not use comments to document the schema. Instead of using comments, you can use the
description or
altdesc attributes available on most schema elements.
Although it is not useful in most cases, you can access a comment by using one of the following schema-dependent paths:
Note:
InterSystems IRIS removes all comments when it reads in XML files. The only comments that can be present are comments that have been added since the XML file was read. To add a comment, use
setValueAt() with a path like one shown in the preceding table.
When you use
setValueAt(), you can specify a value that consists of mixed content (that is, a value that consists of a mix of element and text nodes). For example:
set mixed="SOME TEXT<HOMETOWN>BELMONT</HOMETOWN>"
set status=target.SetValueAt(mixed,"Address")
The following table describes how InterSystems IRIS handles the value in different scenarios:
This section describes variations of virtual property paths that apply when you are referring to a repeating element.
If the path refers to a repeating element, you can use the following syntax to iterate through every instance of that element.
Suppose that we now use a data transformation that contains only the following code:
set status=target.SetValueAt("REPLACED COLOR","FavoriteColors()")
if 'status {do $system.Status.DisplayError(status) quit}
This line of code transforms the document shown previously in this chapter to the following:
<?xml version="1.0" ?>
<Patient MRN='000111222' xmlns='http://myapp.com'>
<Name>Georgina Hampton</Name>
<FavoriteColors>
<FavoriteColor>REPLACED COLOR</FavoriteColor>
<FavoriteColor>REPLACED COLOR</FavoriteColor>
</FavoriteColors>
<Address>
<Street>86 Bateson Way</Street>
<City>Fall River</City>
</Address>
<Doctor>
<Name>Dr. Randolph</Name>
</Doctor>
</Patient>
If the path refers to a repeating element, you can use the following syntax to return the number of elements.
The following table shows example paths for the document shown previously in this chapter:
It can be useful to test virtual document property paths in the Terminal before using them in business processes, data transformations, and so on, particularly when you are getting familiar with the syntax. To do so for schema-dependent XML paths, do the following:
-
-
Use the Management Portal to find the DocType value for the root element of the documents that you plan to test. For example:
-
In the Terminal or in test code:
-
Create a string that contains the text of a suitable XML document.
-
-
Set the
DocType property of this instance.
-
The following method demonstrates step 3:
ClassMethod TestSchemaPath()
{
set string="<Patient xmlns='http://myapp.com'>"
_"<Name>Jack Brown</Name>"
_"<Address><Street>233 Main St</Street></Address>"
_"</Patient>"
set target=##class(EnsLib.EDI.XML.Document).ImportFromString(string,.status)
if 'status {do $system.Status.DisplayError(status) quit}
//Use the DocType displayed in the Management Portal
set target.DocType="MyApp:Patient"
set pathvalue=target.GetValueAt("Address.Street",,.status)
if 'status {do $system.Status.DisplayError(status) quit}
write pathvalue
}
The following shows output from this method:
SAMPLES>d ##class(Demo.CheckPaths).TestSchemaPath()
233 Main St
Content Date/Time: 2019-02-16 01:08:17