04-06-2020 02:22 AM
Hi, I am using XML Parser, and I need to know the position of a child, Is it possible?
I have tried with native nodes and using XPath expressions, but I have not succeded. Does anybody have an idea?
Thank a lot.
04-06-2020 03:12 AM
Use XPath to get nodes, then use Previous Sibling until it returns an error.
This will return 1, 3, 5, 8 (Array). You might expect 0, 1, 2, 3, but sadly XML isn't that easy. Text nodes (the \r\n) are siblings too, and are counted. You can keep count selectively, counting only elements (Array 2):
But if you get the Nth child, text childs are included.
Not sure how performant this is. But if there where a method for this, it would probably do the same.
04-06-2020 03:14 AM - edited 04-06-2020 03:16 AM
Also, you can do this recursively for each level. Get the child index, try to get the parent node, until this gives an array. This will give an array of indices, one for each level.
EDIT: Of course, it's recommended you put these functions in subVIs...
04-06-2020 07:47 AM - edited 04-06-2020 07:58 AM
Hi wiebe@CARYA, thank a lot for the answer.
Please, check the following example:
My intention is to be able to obtain position of the child, to avoid errors when editing the XML code.
Thank a lot. I hope it is clarifying.
04-06-2020 08:03 AM
04-06-2020 08:21 AM
No, the XML code shown in the example is very small, when the XML code increases, it becomes impossible to maintain this.
The index I need matches the postion index of each child. The question is: Is it possible to get this value using nodes or XPath?
Thank a lot.
04-06-2020 09:43 AM
@dgd.gomez wrote:
No, the XML code shown in the example is very small, when the XML code increases, it becomes impossible to maintain this.
The index I need matches the postion index of each child. The question is: Is it possible to get this value using nodes or XPath?
I don't think so.
You can get the position from a XPath result (as I showed earlier).
Or, get all childs (or tables) and iterate over them. The 'i' will be the index.
04-06-2020 04:36 PM
04-06-2020 05:19 PM
Sorry, I still working with LV2017.
04-06-2020 05:22 PM