Set the value of a node

Verified
Added by iNTERFACEWARE

Use node.setNodeValue() to set the value of a leaf node, works for all node types

Source Code
   local Msg = hl7.message{vmd='demo.vmd', name='ADT'}
   
   -- NO BENEFIT: set the Time in the MSH segment
   Msg.MSH[7][1] = os.date('%Y%m%H%m')             -- works: assigns value to the child node
   Msg.MSH[7][1]:setNodeValue(os.date('%Y%m%H%m')) -- works: set the node value
   
   -- BENEFIT: if you decide to use a variable
   local MsgTime = Msg.MSH[7][1]
   MsgTime:setNodeValue(os.date('%Y%m%H%m'))       -- works: set the node value   
   MsgTime = os.date('%Y%m%H%m')                   -- fails: assigns the date to the variable (try it)
Description
Use node.setNodeValue() to set the value of a leaf node, works for all node types
Usage Details

Use node.setNodeValue() to set the value of a leaf node. Often assignment works instead (and is more succinct), however there are occasions when using node.setNodeValue() is useful, for example if you place a node into a variable (see the sample code).

Note: The node.setNodeValue() function works with all types of node trees.

How to use the snippet:

  • Paste the code into your script