Parse XML document with XPath

Parsing a XML document with XPath, I’m able to extract a table:

	{(NSArray) {(NSXMLElement) <tr><th>from</th><th>to</th><th>Modification</th><th> </th><th> </th>
	</tr>, (NSXMLElement) <tr><td> </td><td> </td><td>2021-05-31 21:47:52 </td><td><a href="edit-alias.php?;"></a></td><td><a href="delete.php?;" onclick="return confirm ('effacer\nAlias:')"></a></td>
	</tr>, (NSXMLElement) <tr><td> </td><td> </td><td>2022-09-04 13:30:44 </td><td><a href="edit-alias.php?;"></a></td><td><a href="delete.php?;" onclick="return confirm ('effacer\nAlias:')"></a></td>
	</tr>, (NSXMLElement) <tr><td> </td><td> </td><td>2020-01-30 20:59:20 </td><td><a href="edit-alias.php?;"></a></td><td><a href="delete.php?;" onclick="return confirm ('effacer\nAlias:')"></a></td>
	</tr>}, missing value}

But cant figure out how to get the values of the 1st column of this table (the 'from' column).

Any clue would be much appreciated.

I’m assuming you used something like:

nodesForXPath_error_("//body/table/tr", reference)

to get that array of <tr> elements ?

Since you’re dealing with a collection, it’s potentially easier grab the parent element collectively for all items, and since they will all share the same parent element, you can write the next XPath relative to any one of these (the first being the most obvious candidate):

use framework "Foundation"
property _ref : reference
# Whatever means you used to obtain the array of elements, e.g.
tell the nodesForXPath_error_("//body/table/tr", _ref)

		# Grab the parent of the array's first item
		# (they all have the same parent element)
		tell item 1 to if it ≠ missing value then ¬
			tell its (|parent|'s firstObject())
				# Perform an XPath query relative to the parent
				# i.e. relative to the <table> element.   This
				# returns a collection that can be operated on
				# together to obtain the contents of the first
				# <td> node in each row.
				tell nodesForXPath_error_("./tr/td[1]", _ref)
					tell item 1 to if it ≠ missing value then ¬
						return its stringValue as list
						--> {"", ¬
						--   "", ¬
						--   "", ... }
				end tell
		end tell
end tell

A big thanks to you. You helped me a lot.
I’ve re-written a bit your code especially for errors dialogs.
It’s returning the first column of the first table encountered in a web page.

use framework "Foundation"
use scripting additions

tell application "Safari" to set theXML to source of front document
set theXML to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(missing value)
if theXML = missing value then my displayError:"Impossible de récupérer la page active au forlat XML."

set allTables to (theXML's nodesForXPath:"//table" |error|:(missing value))
if allTables = missing value then my displayError:"Safari n'a renvoyé aucun tableau."
set theTable to allTables's firstObject()

return ((theTable's nodesForXPath:"./tr/td[1]" |error|:(missing value))'s valueForKey:"stringValue")

on displayError:errMess
	tell application "Safari" to display alert "La lecture des alias a échoué :" message errMess buttons {"Annuler"} cancel button 1
end displayError: