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]
- Create a Channel with the the following settings:
- Source = From Translator
- Destination = To Translator
- Channel name = Set XML TEXT to space
- Click the Add Channel button to create the channel.
Ignore the red warning messages, see resolving the milestone configuration error. - Open the Translator by clicking the Edit Script link at the bottom of the To Translator tab.
- 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. - Iguana will load the project and data into the Translator, your screen should look like this:
Tutorial Instructions [top]
- 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
- Iguana automatically passes the message data to the
- Demonstrate the issue.
- Open both XML traces by clicking on the document annotations:
- Comparing both traces you can see the that setInner() has converted the space to an empty string:
- Expanding social-security-no shows that the TEXT element node has been removed:
- Open both XML traces by clicking on the document annotations:
- Use the
node.setText()
function from the xml module to resolve the issue.- Add these two lines of code at the end of
main()
:
- Open the XML trace for
setInner()
on line 14. - Inspecting the dialog shows that social-security-no is now set to a space (‘ ‘):
- Expanding social-security-no shows the TEXT node that contains the space:
- Add these two lines of code at the end of
- 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]
- See our other interface tutorials in this section
- See our general tutorials section
- See our code samples section