This post was originally written for Iguana 5 so it contains version 5 screenshots, and may contain out of date references.
This article addresses an error that occurs when reading an empty XML TEXT element. The issue is a simple one: If you have an XML element with an empty string then the node:nodeValue()
function raises an “Index N is out of bounds.” error, when the desired behaviour is to return an empty TEXT node.
We show you our recommended Best Practice solution, which uses the node:text()
function, from XML Techniques in the Protocols Repo. For more information and other solutions, see: Prevent errors reading from an empty XML element.
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 = Read empty XML TEXT
- Click the Add Channel button to create the channel.
Ignore the red warning messages, see resolving the commit configuration error. - Open the Translator by clicking the Edit Script link at the bottom of the To Translator tab.
- Download and import the Read_empty_XML_TEXT_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
- Navigate through the sample data to see errors caused by empty TEXT elements.
Click next message button on the toolbar five times:
You will see “Index 1 is out of bounds.” errors like this:
- Demonstrate that
:nodeValue(
) is not causing the problem.- Delete “:nodeValue()” from lines 7, 9 and 11
- Your code should look this:
- Navigate through the sample data again, to confirm the same errors occur.
- Conclusion: The error is caused by referencing an empty TEXT element.
- I’m still a bit confused! What exactly is causing this error?
- Navigate to the second message.
- Click the Data and XML annotations, to view their data dialogs:
- You can clearly see the empty TEXT element in both dialogs:
- Expand the “last-name” and “social-security-no” elements.
You can clearly see that the there no TEXT element under “social-security-no”:
- Conclusion: The error is caused by trying to reference the non-existent TEXT element for “social-security-no”.
- Use the
node.text()
function from the xml module to resolve the issue.- Replace “[1]” with “:text()” on lines 7, 9, and 11.
Your code should look like this:
- Navigate through the sample data, to confirm that no errors occur.
- Replace “[1]” with “:text()” on lines 7, 9, and 11.
- Change the code to return strings, like the original code.
- Notice that the
:text()
method is returning TEXT elements:
- Append “:nodeValue()” to lines 7, 9, and 11:
- As you can see we are now returning strings, like the original code.
Finished code:
Original code:
- Notice that the
- 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) -- ORIGINAL CODE: Will cause errors with empty TEXT elements --local FirstName = XML.patients.patient["first-name"][1]:nodeValue() --local LastName = XML.patients.patient["last-name"][1]:nodeValue() --local SSN = XML.patients.patient["social-security-no"][1]:nodeValue() -- FINAL CODE: Does not cause errors with empty TEXT elements local FirstName = XML.patients.patient["first-name"]:text():nodeValue() local LastName = XML.patients.patient["last-name"]:text():nodeValue() local SSN = XML.patients.patient["social-security-no"]:text():nodeValue() end
More Information [top]
- See our other interface tutorials (Iguana 5 documentation) in this section
- See our general tutorials (Iguana 5 documentation) section
- See our code samples (Iguana 5 documentation) section