Set an XML node to a space " " character

Verified
Added by iNTERFACEWARE

Demonstrates the best way to set a XML node to a space character, by using setText(' ') from the xml.lua module

Source Code
require 'xml'

function main(Data)
   -- Parse the XML message
   local XML = xml.parse(Data)
   
   -- Setting social-security-no to a string value works as desired
   local SSN = XML.patients.patient["social-security-no"]:setInner('confidential')
   trace(XML)
   
   -- Setting social-security-no to a space does not work as desired
   -- setInner() parses the string and truncates spaces 
   -- it also removes the TEXT element node (beneath social-security-no)
   local SSN = XML.patients.patient["social-security-no"]:setInner(' ')
   trace(XML)
   
   -- RECOMMENDED BEST PRACTICE: use setText(' ')
   -- Setting social-security-no to a space works as desired
   -- setText() sets the TEXT element node to a space (or any string value)
   -- it also appends a TEXT element node when one does not exists
   local SSN = XML.patients.patient["social-security-no"]:setText(' ')
   trace(XML)
end
Description
Demonstrates the best way to set a XML node to a space character, by using setText(' ') from the xml.lua module
Usage Details

This code addresses a problem when writing a space to an XML TEXT element. The issue is a simple one: If you use the node:setInner(" ") function to set an XML node to a space (” “), it is instead set to an empty string (“”).

We show you our recommended Best Practice solution, which uses the node:setText() function, from the xml custom module.

How to use the code:

  • Use a Filter or To Translator script
    • Add the code
    • Load the sample messages from SampleData.txt
      Note: You will need to split the six XML messages manually, as they are loaded as a single “message” when you import the the file into sample data
  • Alternatively you can also load the attached project which already contains the separated XML sample messages
  • Inspect the code and annotations to see how it works

Note: This code is explained in detail in the How to set an XML node to a space ” ” character tutorial.