Page 1 of 1

Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 9:52 am
by jasonx
Hey Everyone,

I have an XML dataset with operations. I need to search the operations to see if a step is present. My current issue is that the position of this XML element can move based on the job types.

My current build path is:

Code: Select all

[Metadata.Text:Path="/Job/Operations/Operation[1]/Name",Dataset="Xml",Model="XML",Search="Eyelet"]
I would like something like

Code: Select all

[Metadata.Text:Path="/Job/Operations/Operation[*]/Name",Dataset="Xml",Model="XML",Search="Eyelet"]
At the moment my solution is to use a bunch of OR statements and do like the following:

Code: Select all

[Metadata.Text:Path="/Job/Operations/Operation[1]/Name",Dataset="Xml",Model="XML",Search="Eyelet"] OR
[Metadata.Text:Path="/Job/Operations/Operation[2]/Name",Dataset="Xml",Model="XML",Search="Eyelet"] OR
[Metadata.Text:Path="/Job/Operations/Operation[3]/Name",Dataset="Xml",Model="XML",Search="Eyelet"] OR
etc
which isn't elegant.

I could also build a nodejs script to traverse the XML node at /Job/Operations/Operation and search and simply return a true or false to go to the next step in the flow and set the value as private data.

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 10:37 am
by r.zegwaard
Hi

If you do it like this,

Code: Select all

[Metadata.Text:Path="//Operation[1]/Name",Dataset="Xml",Model="XML",Search="Eyelet"]
then it should find the first Operation/Name, no matter where in the XML.


Robert

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 10:49 am
by laurentd
Use Metadata.TextIndexed, it will return you all Name values in one go.
[Metadata.TextIndexed:Path="/Job/Operations/Operation/Name",Dataset="Xml",Model="XML"]
Then use a condition to check if it contains the string you want.

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 10:58 am
by freddyp
Different approach: instead of selecting nodes by their index, you can also select them based on certain children elements and/or attributes. So instead of writing

Code: Select all

/Job/Operations/Operation[1]/Name
you can write

Code: Select all

/Job/Operations/Operation[Name='Eyelet']/Name
Note the single quotes! Double quotes are allowed in XPath but because the XPath is enclosed in the double quotes of the Switch variable you must use singles quotes here.

If the node does not exist, the value of that XPath in Switch will be empty so you can check for the result of that XPath not being empty: use the operator "Matches" with .+ as the right-hand value.

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 11:02 am
by jasonx
Hey Robert the position is not always [1]. It varies job to job.

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 11:03 am
by jasonx
Lauren your solution worked.

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 11:06 am
by jasonx
freddyp: The XML data in that node isn't always eyelet there's a range of methods but they all contain eyelet. This step in my flow is to inject an automated note about how many eyelets etc are required for a job into our MIS system. I need to be able to search all nodes for eyelet in this instance then retrieve the data from this node. Laurens solutions solve that issue for me.

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Mon Jan 24, 2022 6:18 pm
by freddyp
I had no doubts that Laurent's solution would work, his solutions always do :D. I just suggested an alternative based on XPath to draw the forum users' attention to the potential of XPath. I think that many of you can benefit from getting to know it a bit better. To refine that previous XPath a bit further just to illustrate that (I hope it is self-explanatory):

Code: Select all

/Job/Operations/Operation[contains(Name, 'Eyelet') or contains(AnotherNode, 'Grommet')]/Name

Re: Is using a wildcard in an XML path for a flow condition possible?

Posted: Tue Jan 25, 2022 2:39 am
by jasonx
Thanks freddyp,

Before I read your reply I did more reading on Xpath and came up with:

Code: Select all

[Metadata.TextIndexed:Path="/Job/Operations/Operation/Name[contains(text(),'Eyelet')]",Dataset="Xml",Model="XML"]
Based on what you suggested earlier. This is so I can search for the node and also get its value at the next step. I wasn't suggesting what you said was wrong I just didn't have the knowledge with Xpath. But you've brought this to my attention for something to learn further.

I hope I can gain more knowledge and pay it forward in the future.