04-29-2012 04:26 AM - edited 03-14-2019 09:46 AM
Hello Support Cummunity,
i have a XML file with the following content:
<csv_data_records>
<record>
<title/>
<firstname>numberone</firstname>
<lastname>numberone</lastname>
<phone>11111</phone>
</record>
<record>
<title/>
<firstname>number2</firstname>
<lastname>number2</lastname>
<phone>22222</phone>
</record>
<record>
<title/>
<firstname>number3</firstname>
<lastname>number3</lastname>
<phone>33333</phone>
</record>
</csv_data_records>
my "Get XML Document Data" looks like this: "/descendant::csv_data_records/child::record/child::phone"
in the first round it returns 11111 as phone number, thats ok, if this doesnt match to the caller i make a loop and make the "Get XML Document Data" a second time, but then it also returns 11111.
i remember it from the UCCXD Course that this works if i use a SQL Server. but why doesn't it work with XML?
with SQL Server it recognizes itself that this is the second query and returns the second value...
is there a hint to also achieve this with XML?
04-29-2012 05:27 PM
You have to use an XPATH predicate like this:
//record[1]
//record[2]
etc.
Therefore, your xapth needs to be built at run time like this:
label Get Next Record
increment i
xpath_record = "//record[" + i + "]"
xpath_result = Get XML Document Data (xml_doc, xpath_record)
if (xpath_result != null && xpath_result.trim() != "")
true
/* Data Found - Store the data in the record variable */
set record = xpath_result
...Your record processing steps go here...
goto Get Next Record
false
/* No Data Found - End the loop */
goto End of Record Set
label End of Record Set
Source for more info: http://www.w3schools.com/xpath/xpath_syntax.asp
EDIT: Added more detail to the script example.
04-29-2012 11:48 PM
ok thank you Anthony, i will try that.
04-30-2012 10:50 AM
Please write back and let everyone know if it worked, and if so, mark this solution as solved so others know if this is a good solution to follow or not. Thanks.
01-27-2013 12:34 PM
This doesn't seem to work for me. I have tried several variations of XPath, but [2] or more always comes up "null", even though the XML Document contains multiple records.
When calling record[1]. I pull up the first data. No problems. but, calling record[2] or record[3] or more, always just shows up null data.
I've tried using the absolute path (i.e. /descendant::root/child::childnode/child::datafield[2]). i've also tried ( //datafield[2] ).
None seem to work. Always come up null. But, when i debug, and I look at the XML Document that I'm parsing, I verified all the nodes are there.
01-27-2013 01:31 PM
I found the problem. Using this XML as example:
Boring Stuff
Boring Stuff2
Cool Stuff
Cool Stuff2
Now, using //data1[1] and using //data1[2] should work, but it didn't for me. Instead, I had to use the array at the "Record" level rather than the data level. In other words, to get both "Boring Stuff" and "Cool Stuff", I had to use the following XPath:
/descendant::Parent/child::Record[1]/child::data1
and
/descendant::Parent/child::Record[2]/child::data1
So, if I were to modify Anthony's script above:
label Get Next Record
increment i
xpath_record = "/descendant:Parent/child::Record[" + i + "]/child::data1"
xpath_result = Get XML Document Data (xml_doc, xpath_record)
if (xpath_result != null && xpath_result.trim() != "")
true
/* Data Found - Store the data in the record variable */
set record = xpath_result
...Your record processing steps go here...
goto Get Next Record
false
/* No Data Found - End the loop */
goto End of Record Set
label End of Record Set
06-22-2015 10:28 AM
The i increment should work if you need an actual exemple:
01-19-2018 08:15 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide