How to create dynamic xpath with namespace for xml value

Hi,

Can anyone help on the “Execute XPath” - “XPathExpression”?

I’d like to obtain the value of element “cbc:ID” as shown below (in yellow):
image

The challenges here are:

  • All elements are tagged with namespace (See attachement)
  • Semi-dynamic structure. The main tree structure does not change largely, however small change may occur. For instance the element “cac:Party” maybe gone. And “cac:Contact” becomes the direct child of “cac:AccountingCustomerParty”

I’ve tried many suggestions such as:

  • xElement.Descendants.Where(function(n)n.Name.LocalName = “ID”).value
    • However, it will not solve issue when there are multiple element “ID” from the same xml doc)
  • The UiPath official web where sample XPathExpression is “string(/ele1/ele2/…/text()[1])”
    • When I add the namespace in the path. Error prompts “Execute XPath: … has an invalid token”

BR
Helliton

Dummy Invoice.xml (982 Bytes)

@hellitonwoo
Have a look on following posts dealing XML and namespaces

@ppr Thanks a lot for the quick reply.

The method using “xdoc.element” or “xdoc.desendants” are usful in other cases.
But, in my case the structure changes may only be resolved by XPath (from what I’ve been digging so far).
Somthing like: string(/path/to/[local-name()=‘ele_name1’/[local-name()=‘ele_name2’]/text()[1]), where there could be a number of elements between “ele_name1” and “ele_name2”.
The challenge here is that XPathExpression won’t return any value.

Thanks!

@hellitonwoo
Give a try on Something Like this
xpath: “string(//*[local-name()=‘YourElementName’]/text()[1])” would return the value.

Just to Show in how to Deal with xpath and localname in namespace used scenario

@ppr
I think I got it

To have a dynamic path, the “//*” can be inserted between each “local-name”, for instance:
XPathExpression as: "string(//*[local-name()=‘Element1’]//*[local-name()=‘Element2’]//*[local-name()=‘Element3’]/text()[1])"

As long as the following cretiria is met, the value can be retrieved:

  • Element2 is desendant of Element1
  • Element3 is desendant of Element2

Thanks!

@hellitonwoo
Perfect, yes the * is like a wildcard to the namespace. When its working so just flag the solving post as solution. Other forummembers can so benefit from it. Thanks

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.