Page 1 of 1

XPath expression issue

Posted: Mon Feb 10, 2025 11:32 pm
by mclarke
Okay, so I haven't worked with XPath in a version or two, but the command I used to use to parse XML files via an expression doesn't work. I even copied the syntax from the help page and it still gives me an error. I'm sure it must be something that I don't remember, or didn't write down in my notes. Could someone please look at the attached screenshot and tell me what I'm doing wrong?
XPATH Define single-line text with variables_ Copies-right.png
XPATH Define single-line text with variables_ Copies-right.png (29.25 KiB) Viewed 32329 times

Re: XPath expression issue

Posted: Tue Feb 11, 2025 12:15 am
by jan_suhr
Have you tried with XML location path selected Instead

Re: XPath expression issue

Posted: Tue Feb 11, 2025 1:56 am
by cstevens
Wouldn't the XPath for that just be //JobGang/JobID/@text ?

Re: XPath expression issue

Posted: Tue Feb 11, 2025 8:10 am
by freddyp
As you are not using an XPath expression the option to work with "XML location path" is indeed easier. When choosing that way of working you can click your way through the structure of the XML and the XPath will build itself. For easy XMLs like the one you have that is more than good enough.

Re: XPath expression issue

Posted: Tue Feb 11, 2025 5:30 pm
by mclarke
I tried just following down the XPath and it gave me a correct value. I can use that process for the rest of the XML values.

Any reason(s) why my original expression wouldn't work? I know I've used it before and I copied it right out of the Switch Help doc on XPath.

Re: XPath expression issue

Posted: Tue Feb 11, 2025 11:17 pm
by loicaigon
mclarke wrote: Tue Feb 11, 2025 5:30 pm Any reason(s) why my original expression wouldn't work? I know I've used it before and I copied it right out of the Switch Help doc on XPath.
Well the doc states:

//field[name="JobID"]/value
selects all value elements in the document that have a parent field element and a sibling name element with string-value equal to JobID

That would work if the structure would be somehow similar to:

Code: Select all

<job>
  <field>
    <name>JobID</name>
    <value>123</value>
  </field>
</job>
Now your XML structure is structured in a different way. So the XPath you want to use must adapt to that. I am not sure here what you need to catch but if that was the Amount, you could indeed get to use something similar to the doc example:
//JobGang[name="JobID"]/Amount

Re: XPath expression issue

Posted: Sat Apr 05, 2025 6:16 am
by RoseBL
loicaigon wrote: Tue Feb 11, 2025 11:17 pm
Well the doc states:

//field[name="JobID"]/value
selects all value elements in the document that have a parent field element and a sibling name element with string-value equal to JobID

That would work if the structure would be somehow similar to:

Code: Select all

<job>
  <field>
    <name>JobID</name>
    <value>123</value>
  </field>
</job>
Now your XML structure is structured in a different way. So the XPath you want to use must adapt to that. I am not sure here what you need to catch but if that was the Amount, you could indeed get to use something similar to the doc example:
//JobGang[name="JobID"]/Amount
Can you explain more clearly how to write XPath correctly if the XML has more complex nesting levels than this example?

Re: XPath expression issue

Posted: Sat Apr 05, 2025 10:25 am
by loicaigon
Hello,

XPath is very rich and my post alone won't be sufficient to give you all the keys.

Might be best that you have a look at teh numerous tutorials you can find online:
https://www.w3schools.com/xml/xpath_intro.asp
Jump here if you are already familiar with basics:
https://www.w3schools.com/xml/xpath_syntax.asp

You have this one as well:
https://devhints.io/xpath

And this can be used to test expressions online:
https://xpather.com/