LoadFromRecord (XmlDoc/XmlNode subroutine): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 12: Line 12:
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%doc</th><td><var>LoadFromRecord</var> returns a new <var>XmlDoc</var> object.</td></tr>
<tr><th>doc</th><td><var>LoadFromRecord</var> is in both the <var>XmlDoc</var> and <var>XmlNode</var> classes.
When the method object is an <var>XmlDoc</var>, or refers to the <var>Root</var> node of an <var>XmlDoc</var>: <ul> <li>The <var>XmlDoc</var> must not contain any nodes except the <var>Root</var> node. <li>A "Record" element is added as the top level element of the <var>XmlDoc</var>. Children are added to the Record element, representing the outer fields and fieldgroups of the record. </ul>
When the method object is an <var>XmlNode</var> not referring to the <var>Root</var> node of an <var>XmlDoc</var>:
<ul>
<li>The node must be an <var>Element</var> node.
<li>Children are added to that element, representing the outer fields and fieldgroups of the record.
<li>See the [[#AddToRecord subroutine in XmlDoc class|AddToRecord subroutine description]] for some examples of extracting multiple records into a single <var>XmlDoc</var>.
</ul>
See the <var>AddToRecord </var> section [[AddToRecord (XmlDoc subroutine)#Structure of XmlDoc for AddToRecord|"Structure of XmlDoc for AddToRecord"]] for a description of the <var>XmlDoc</var> created by <var>LoadFromRecord</var>.
</td></tr>
<tr><th><var>%(XmlDoc)</var></th>
<tr><th><var>%(XmlDoc)</var></th>
<td>The class name in parentheses denotes a shared method and is one way to invoke <var>LoadFromRecord</var>. You can also use an object expression whose type is <var>XmlDoc</var> (even if the value of the expression is null).</td></tr>
<td>The class name in parentheses denotes a shared method and is one way to invoke <var>LoadFromRecord</var>. You can also use an object expression whose type is <var>XmlDoc</var> (even if the value of the expression is null).</td></tr>
Line 71: Line 82:
<tr><th><var>AllowNull</var></th>
<tr><th><var>AllowNull</var></th>


<td>The value of this name required <var>Boolean</var> argument, which defaults to <var>False</var>, is copied to the <var>[[AllowNull (XmlDoc property)|AllowNull]]</var> property of the <var>XmlDoc</var> created by <var>LoadFromRecord</var>. The <var>XmlDoc</var>'s <var>AllowNull</var> property, in turn, determines whether field values that contain the X'00' character are stored in the <var>XmlDoc</var> with base64 encoding. Such values are base64 encoded if the <var>AllowNull</var> property is <var>False</var> (the default). For an example, see [[#Handling records with null characters|"Handling records with null characters"]], below.
<td>The value of this name required <var>Boolean</var> argument, which defaults to <var>False</var>, is copied to the <var>[[AllowNull (XmlDoc property)|AllowNull]]</var> property of the <var>XmlDoc</var> created by <var>LoadFromRecord</var>. The <var>XmlDoc</var>'s <var>AllowNull</var> property, in turn, determines whether field values that contain the X'00' character are stored in the <var>XmlDoc</var> with base64 encoding. Such values are base64 encoded if the <var>AllowNull</var> property is <var>False</var> (the default). For an example, see the <var>NewFromRecord</var> [[NewFromRecord (XmlDoc function)#Handling records with null characters|"Handling records with null characters"]] section.
   
   
</td></tr>
</td></tr>
Line 102: Line 113:
<li>All fields are copied to the <var>XmlDoc</var> by <var>LoadFromRecord</var>.
<li>All fields are copied to the <var>XmlDoc</var> by <var>LoadFromRecord</var>.
   
   
<li>See the [[#AddToRecord subroutine in XmlDoc class|AddToRecord subroutine description]] for an example of extracting a record to an <var>XmlDoc</var> for record copying, and see the <var>[[ToXmlDoc (Record function)|ToXmlDoc]]</var> function in the <var>[[Record class|Record]]</var> class for other examples using the method arguments.
<li>See the [[#AddToRecord subroutine in XmlDoc class|AddToRecord subroutine description]] for an example of extracting a record to an <var>XmlDoc</var> for record copying, and see the <var>[[ToXmlDoc (Record function)#Examples|ToXmlDoc]]</var> function for other examples using the method arguments.
   
   
<li><var>LoadFromRecord</var> was actually introduced in version 7.6 of
<li><var>LoadFromRecord</var> was actually introduced in version 7.6 of
the <var class="product">Sirius Mods</var>, but the <var>XmlNode</var> class <var>LoadFromRecord</var> implementation prior to version 7.8 required that the <var>XmlDoc</var> be empty.
the <var class="product">Sirius Mods</var>, but the <var>XmlNode</var> class <var>LoadFromRecord</var> implementation prior to version 7.8 required that the <var>XmlDoc</var> be empty.
</ul>


</ul>
==Examples==
==Examples==
 
====Copying from multiple source records====
Since <var>LoadFromRecord</var> subroutine can
extract from a record into a subtree of an <var>Element</var> node in an <var>XmlDoc</var>,
you can use it, together with <var>AddToRecord</var>, to combine multiple
source records into one target record.
As a simple example, you can put the fields from two records
&ldquo;side by side&rdquo; into a target record:
<p class="code"><nowiki>FRN %x
  %doc = %doc:NewFromRecord
End For
%top = %doc:SelectSingleNode('*')
FRN %y
  %top:LoadFromRecord
End For
Store Record
End Store
%rn = $CurRec
FRN %rn
  %doc:AddToRecord
End For
</nowiki></p>
The <var>XmlDoc</var> that is input to <var>AddToRecord</var> would have a structure
similar to the following:
<p class="code"><nowiki><Record ...>
  <field ...>
  ... first field from record %x
  </field>
  ...
  <field ...>
  ... first field from record %y
  </field>
  ...
</Record>
</nowiki></p>
Of course, you could also accomplish this particular objective by
using two <var>XmlDoc</var> objects:
<p class="code"><nowiki>FRN %x
  %doc1 = %doc1:NewFromRecord
End For
FRN %y
  %doc2 = %doc2:NewFromRecord
End For
Store Record
End Store
%targ = $CurRec
FRN %targ
  %doc1:AddToRecord
  %doc2:AddToRecord
End For
</nowiki></p>
You can also use <var>LoadFromRecord</var> to modify the fieldgroup structure
within a record (using V7R2 of <var class="product">Model 204</var>).
For example, you could take the &ldquo;outer&rdquo; fields of one
record and move them to be fieldgroup members in the target
record:
<p class="code"><nowiki>In SRCFILE FRN %x
  %doc = %doc:NewFromRecord
End For
%fg = %doc:SelectSingleNode('*/fieldgroup[@name="GRP"]')
In SRCFILE FRN %y
  %fg:LoadFromRecord
End For
In TARGFILE Store Record
End Store
%rn = $CurRec
In TARGFILE FRN %rn
  %doc:AddToRecord
  PAI
End For
</nowiki></p>
The use of a separate source and target file above (which in
general is a typical usage of the record copying methods) is
more or less required here, because the fields in <code>SRCFILE</code> are
defined as outer fields, but in <code>TARGFILE</code> they are defined as members of fieldgroup
<code>GRP</code> (or they would need to be &ldquo;FIELDGROUP *&rdquo; fields).
So, for example, definitions for <code>SRCFILE</code> might include:
<p class="code"><nowiki>DEFINE FIELD FOO
DEFINE FIELDGROUP GRP
</nowiki></p>
and definitions for <code>TARGFILE</code> might include:
<p class="code"><nowiki>DEFINE FIELDGROUP GRP
DEFINE FIELD FOO WITH FIELDGROUP GRP
</nowiki></p>
And the <var>XmlDoc</var> that is input to the above <var>AddToRecord</var> subroutine
may look like:
<p class="code"><nowiki><Record ...>
  <fieldgroup name="GRP" ...>
      <field name="FOO">
        value of foo
      </field>
  </fieldgroup>
</Record>
</nowiki></p>
And the corresponding output of the above <code>PAI</code> would be:
<pre>
    \GRP = 1
    FOO = value of foo
    /GRP = 1
</pre>




{{Template:XmlDoc:LoadFromRecord footer}}
{{Template:XmlDoc:LoadFromRecord footer}}

Revision as of 21:12, 2 June 2011

Template:XmlDoc:LoadFromRecord subtitle

This subroutine adds to an XmlDoc object a subtree that contains the fields and fieldgroups from the current record. This record extraction is the same operation that is performed by the NewFromRecord shared function and by the Record class ToXmlDoc function.

Syntax

Template:XmlDoc:LoadFromRecord syntax


Syntax terms

docLoadFromRecord is in both the XmlDoc and XmlNode classes. When the method object is an XmlDoc, or refers to the Root node of an XmlDoc:
  • The XmlDoc must not contain any nodes except the Root node.
  • A "Record" element is added as the top level element of the XmlDoc. Children are added to the Record element, representing the outer fields and fieldgroups of the record.

When the method object is an XmlNode not referring to the Root node of an XmlDoc:

  • The node must be an Element node.
  • Children are added to that element, representing the outer fields and fieldgroups of the record.
  • See the AddToRecord subroutine description for some examples of extracting multiple records into a single XmlDoc.

See the AddToRecord section "Structure of XmlDoc for AddToRecord" for a description of the XmlDoc created by LoadFromRecord.

%(XmlDoc) The class name in parentheses denotes a shared method and is one way to invoke LoadFromRecord. You can also use an object expression whose type is XmlDoc (even if the value of the expression is null).
AttributeValues This name required argument is a Boolean value that indicates whether a field value will be stored as “XML text” or as an XML attribute (belonging to its field, which is an XmlDoc element).

For example, <APSUBUND>COMM</APSUBUND> is text format, and <APSUBUND value="COMM"/> is attribute value format.

The default value is False, which produces text format. In this format, the value of a field will be converted to base64 in the XmlDoc if the field contains a byte that is:

  • Equal to X'00', if the AllowNull property of the XmlDoc is False.
  • Between X'00' and X'3F'.
  • Not translatable from EBCDIC to Unicode, using either the standard Unicode translation table or the base codepage translation table, as determined by the CodepageTable argument of LoadFromRecord.
  • Not invertible when translating from EBCDIC to Unicode and back to EBCDIC using the standard Unicode translation table, if the CodepageTable argument is False.
This argument must be False if the XmlDoc is to be used as the method object of the AddToRecord subroutine.
AttributeNames This name required argument is a Boolean value that indicates whether each field name is to be stored in the XmlDoc as an element name or as the value of a "name" attribute.

For example, <APSUBUND>COMM</APSUBUND> is element-name format, and the following is name-as-attribute format:

<field name="APSUBUND"> COMM </field>

The default value as of Sirius Mods version 7.6 (and maintenance back to version 7.3) is True, which produces name-as-attribute format. Formerly, the default value was False.

The name-as-attribute format from the True option is better suited to operations on the XmlDoc, particularly a record copying operation. The element-name format from the False option produces more compact output when the XmlDoc is serialized.

This argument must be True if the XmlDoc is to be used as the method object of the AddToRecord subroutine.
NamesToLower This name required argument is a Boolean value that indicates whether field names are stored in all lowercase characters. The default value is False, which does not translate uppercase name characters to lowercase.

If the XmlDoc is to be used for record copying, this argument should probably be False.

AllowUnreversible This name required argument is a Boolean value that indicates whether a request is cancelled if a field name would be changed irreversibly by lowercasing or by replacing with a period the characters that would be invalid in an XML document.

The default value is False, which allows request cancellation to alert you about unreversible field names.

If the XmlDoc is to be used for record copying, this argument should probably be False.
CodepageTable This name required argument is a Boolean value; if True, the translations defined by the base Unicode codepage are used when translating from EBCDIC to Unicode for storing in the XmlDoc.

This argument is for the unusual case where you anticipate that the XML document is to be used later by AddToRecord, and the standard Unicode translation tables in place when AddToRecord is invoked may differ from those in place when the record was copied to the XmlDoc.

The default value is False, which uses the standard Unicode translation tables, including any modifications specified in UNICODE Trans or UNICODE Map commands.

The advantage of using CodepageTable=False is that it will allow you to readily modify the XmlDoc directly (that is, with the AddElement and AddAttribute methods). Those operations will use the standard Unicode translation tables; there is no way to perform them using the base codepage translation tables.
AllowNull The value of this name required Boolean argument, which defaults to False, is copied to the AllowNull property of the XmlDoc created by LoadFromRecord. The XmlDoc's AllowNull property, in turn, determines whether field values that contain the X'00' character are stored in the XmlDoc with base64 encoding. Such values are base64 encoded if the AllowNull property is False (the default). For an example, see the NewFromRecord "Handling records with null characters" section.

Usage notes

  • Whether to use ToXmlDoc, NewFromRecord, or LoadFromRecord depends on what is most convenient for your application. If you are already using a Record object which references the desired record, using ToXmlDoc may be more convenient; if not, then either LoadFromRecord or NewFromRecord (both of which require that the method be contained in a “record loop”) may be more convenient. You must use LoadFromRecord if you want to add the record's content as a subtree to a non-empty XmlDoc; in other cases the NewFromRecord “factory method” may be your choice.

    Since NewFromRecord and ToXmlDoc create new XmlDoc objects, they have the AllowNull argument for setting the created XmlDoc's AllowNull poperty; LoadFromRecord does not have the AllowNull argument.

    As stated, both NewFromRecord and LoadFromRecord must be contained in a “record loop”, for example, an FRN block, and they may not be invoked within a fieldgroup context.

    Except for these considerations, ToXmlDoc, NewFromRecord, and LoadFromRecord all perform the same operation and have the same arguments.

  • All fields are copied to the XmlDoc by LoadFromRecord.
  • See the AddToRecord subroutine description for an example of extracting a record to an XmlDoc for record copying, and see the ToXmlDoc function for other examples using the method arguments.
  • LoadFromRecord was actually introduced in version 7.6 of the Sirius Mods, but the XmlNode class LoadFromRecord implementation prior to version 7.8 required that the XmlDoc be empty.

Examples

Copying from multiple source records

Since LoadFromRecord subroutine can extract from a record into a subtree of an Element node in an XmlDoc, you can use it, together with AddToRecord, to combine multiple source records into one target record.

As a simple example, you can put the fields from two records “side by side” into a target record:

FRN %x %doc = %doc:NewFromRecord End For %top = %doc:SelectSingleNode('*') FRN %y %top:LoadFromRecord End For Store Record End Store %rn = $CurRec FRN %rn %doc:AddToRecord End For

The XmlDoc that is input to AddToRecord would have a structure similar to the following:

<Record ...> <field ...> ... first field from record %x </field> ... <field ...> ... first field from record %y </field> ... </Record>

Of course, you could also accomplish this particular objective by using two XmlDoc objects:

FRN %x %doc1 = %doc1:NewFromRecord End For FRN %y %doc2 = %doc2:NewFromRecord End For Store Record End Store %targ = $CurRec FRN %targ %doc1:AddToRecord %doc2:AddToRecord End For

You can also use LoadFromRecord to modify the fieldgroup structure within a record (using V7R2 of Model 204). For example, you could take the “outer” fields of one record and move them to be fieldgroup members in the target record:

In SRCFILE FRN %x %doc = %doc:NewFromRecord End For %fg = %doc:SelectSingleNode('*/fieldgroup[@name="GRP"]') In SRCFILE FRN %y %fg:LoadFromRecord End For In TARGFILE Store Record End Store %rn = $CurRec In TARGFILE FRN %rn %doc:AddToRecord PAI End For

The use of a separate source and target file above (which in general is a typical usage of the record copying methods) is more or less required here, because the fields in SRCFILE are defined as outer fields, but in TARGFILE they are defined as members of fieldgroup GRP (or they would need to be “FIELDGROUP *” fields). So, for example, definitions for SRCFILE might include:

DEFINE FIELD FOO DEFINE FIELDGROUP GRP

and definitions for TARGFILE might include:

DEFINE FIELDGROUP GRP DEFINE FIELD FOO WITH FIELDGROUP GRP

And the XmlDoc that is input to the above AddToRecord subroutine may look like:

<Record ...> <fieldgroup name="GRP" ...> <field name="FOO"> value of foo </field> </fieldgroup> </Record>

And the corresponding output of the above PAI would be:

    \GRP = 1
     FOO = value of foo
    /GRP = 1


Template:XmlDoc:LoadFromRecord footer