How to set an XML node to a space ” ” character

This article 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. For more information and other solutions, see: Setting an XML node to a space ” ” character.

We recommend that you type in the code for each step, but we also include complete Sample Code if you prefer to paste it in and follow along.

Create the Channel [top]

  1. Create a Channel with the the following settings:
    • Source = From Translator
    • Destination = To Translator
    • Channel name = Set XML TEXT to space
  2. Click the Add Channel button to create the channel.
    Ignore the red warning messages, see resolving the milestone configuration error.
  3. Open the Translator by clicking the Edit Script link at the bottom of the To Translator tab.
  4. Download and import the Set_XML_TEXT_to_space_To_Translator.zip project file.
    This file contains a skeleton project and six sample XML messages.
  5. Iguana will load the project and data into the Translator, your screen should look like this:

Tutorial Instructions [top]

  1. Pass the message data to the script.
    • Iguana automatically passes the message data to the main() function
    • The message can be accessed using the Data parameter
    • No action is needed
  2. Demonstrate the issue.
    1. Open both XML traces by clicking on the document annotations:
    2. Comparing both traces you can see the that setInner() has converted the space to an empty string:
    3. Expanding social-security-no shows that the TEXT element node has been removed:
  3. Use the node.setText() function from the xml module to resolve the issue.
    1. Add these two lines of code at the end of main():
    2. Open the XML trace for setInner() on line 14.
    3. Inspecting the dialog shows that social-security-no is now set to a space (‘ ‘):
    4. Expanding social-security-no shows the TEXT node that contains the space:
  4. That’s it you’re done!

Complete Sample Code [top]

Here is the finished code that you can cut and paste into your script.

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)
     
   -- 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

More Information [top]