Fetch XML node value (with condition on other node values) using Execute Xpath utility in UIPath

In order to fetch XML node value in UIpath I followed below steps.

I read the XML file using Read file utility .
Deserialized the XML using Deserialize XML activity.
in Execute Xpath activity I used the output variable of Deserialize XML as input and below given Xpath.

“Records-Set/record[A = ‘abc’][B=‘123’]/@Leadtime
but while printing it to a msgbox It just prints below given string in the message box.

System.Xml.XPath.XPathEvaluator+d_1`1[System.Object].

Please help me on this .

below is the XML I am using.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> def 456 True 12 abc 123 True 12

hi @KT1993

Check the below posts

Thanks
Ashwin S

Thanks Ashwin

but I already went through all these posts but they could not help me out :frowning:

hi @KT1993
cHECK THIS POST THIS SURELY WILL HELP YOU

IF NOT TELL ME THE EXACT SCENARIO

Thanks
Ashwin S

XML

output

if I use below stateement in Assign activity I can fetch the value of all the attributes under element records as shown in the msgbox screenshot above but instead I just need the value of Leadtime and with a condition where A=“abc”

Copper_DecisionTable_XML.Root.Element(“record”).Value.ToString

I need the XPATH for such scenario . the below given works well in other XML tools
Records-Set/record[A = ‘abc’][B=‘123’]/Leadtime[1]

hi @KT1993

use this xpath in execute xpath

//record[@A][@B][@leadtime]
or use individually

//record[@A]

Thanks
Ashwin S

Thanks Ashwin :slightly_smiling_face:

using below statement in Execute Xpath utility is working fine for me.
“string(/Records-Set/record[A = ‘abc’][B=‘123’]/Leadtime)”

though I have a new challange now , to make this Xpath dynamic . can anyone help me with the syantx for dynamic Xpath…

I used this below given statement but its not working as expected :frowning:
“string(/Records-Set/record[A = '”+var1+"’][B=’"+var2+"’]/Leadtime)"

hi @KT1993

try to use * instead of var1 and var2

Thanks
Ashwin S

@AshwinS2

var1 and Var2 are the name of the variables which will be used to pass the values into statement at runtime.
how can repalcing them with * would work ?

using below statement in Execute xpath utility helped me at last :slight_smile:

“string(/Records-Set/record[A = '”+var1.ToString+"’][B=’"+var2.ToString+"’]/Leadtime)"

Hi ,

Please find below tutorials,

Thanks,
Karthik.

Slightly old thread, but thought I would just put some details in here to help those searching.

using below statement in Execute xpath utility helped me at last :slight_smile:

“string(/Records-Set/record[A = '”+var1.ToString+"’][B=’"+var2.ToString+"’]/Leadtime)"

If the XML looks the same

<Records-Set>
  <record>
    <A>...</A>
    <B>...</B>
    <C/>
    <D/>
    <E>True</E>
    <Leadtime>12</Leadtime>
  </record>
  ...
</Records-Set>

You can shorten your XPATH expression a little by search for //record instead of the ROOT element /Records-Set/record....

So it looks like

"//record[A = '" + var1 + "'][B = '" + var2 + "']/Leadtime"

It’s not much in this particular example, but for deeply nested structures, starting from the root would get messy.

And as you figured out, wrapping your XPATH expression in the String() function returns the string value of the single node, for a parent element string() would return the string values of each of the child nodes.

Friends, im trying to fetch the url from this XML

i tried like this but im not getting correct output

pls help me out

Hi @kgbheem,

To help others to assist you, many find it beneficial when the raw text or an example file to work with where possible, this way someone can spend less time speculating or back and forth banter to mimic the particular scenario.

I mocked up part of the XML from your image

<MultimapPresentation version="1.0.41" client="dealerdirect">
  <locationData type="storefinder">
    <locationCount>1</locationCount>
    <totalRecords>1</totalRecords
    <prevPageUrl />
    <nextPageUrl />
    <location id="1">
      <record>
        <param name="busid">2640</param>
        <param name="zip">06374-2034</param>
        <param name="url">http://www.google.ca</param>
        <param name="preown">1</param>
      </record>
    </location>
  </locationData>
</MultimapPresentation>

In your original XPath Query

This would be searching the XML for any matching <record> nodes that contain with a child-node <name> with a value of url.

The expected XML would look something like the following and would return an Object of matching the query, in this case just the 2nd <record>.

<root>
  <record>
    <name>Bob</name>
  </record>
  <record>
    <name>url</name>
    <age>107</age>
  </record>
</root>

If you were to wrap the query in a string() e.g. string(//record[name='url']) it would return the string instance of each child node’s value concatenated together. e.g. url107.

There are many ways to go about querying with XPath, looking back at your example, the absolute query would be:

//MultimapPresentation/locationData/location/record(0)/param[name='url']

Shortening the query to look for matching <record> that contains <param name="url">... you’re missing a node level along with reference to the attribute.

//record/param[name='url']

finally, you’d want to convert XPath object to a string using the fn:string() function turning the query into

string(//record/param[name='url'])

Alternatively, you can also use the XMLDocument methods such as XPathSelectElements if you didn’t want to use the Execute XPath activity.

Hope that clears it up!

4 Likes

Thank you so much @twhitehead.