Page 1 of 1

find all node value after the current node for a specific node

Posted: Mon Dec 19, 2022 3:13 pm
by SmitaraniB
Hi Team,

I wanted to check if we can get all the node value after the selected node in the sequence they are present to a variable.
Capture_pdc.PNG
in the attachment I am at the selected pdc.number element tag with value 34 and I want all the values in the pdc element tag value sequentially using js file.


Thanks,
Smita

Re: find all node value after the current node for a specific node

Posted: Mon Dec 19, 2022 4:32 pm
by cristi_talau
Hello,

I would try to make a DFS traversal of the DOM tree and start collecting values of the "pdc" element tag after we encounter the currently selected node.
The steps will imply:
  • Obtaining the "#document" element of the XML DOM tree
  • Obtaining the currently selected node
  • Iterating the tree and collecting the desired nodes
If you want to go into more details regarding one of the steps, please let me know.

Another approach is to obtain the "#document" node, and the "getElementsByTagName()" method to obtain your "pdc.number" elements. You can also use Node.compareDocumentPosition(otherNode) to filter the ones that come after the selection.

Best,
Cristian