Querying XML / Tree data

Hi
Is it possible to query a tree structure? All the examples seem to work in the context of wanting to know the values for the an explicitly indexed element, but not where an element(s) match an attribute value.

Is this possible?

thanks

Hey Paul,

Yes you are right we do not have a sensetalk command which allows you to use a value and provide the element / node of it.

Here is a handler which does the job. kudos @SenseTalkDoug :slight_smile:
It will search the whole tree and print each found node that contains the searchtext.

put file resourcepath("samplexml.xml") as tree into universal myTree

put recFunc(universal mytree, "name","code", "42")

to recFunc myTree, SearchTag, SearchAttribute, SearchText	
	put empty list into runscripts
	put empty list into mylist
	
	repeat with each item mynode in myTree		
		put the _tag of mynode into mytag		
		if mytag contains searchtag
			if mynode.(SearchAttribute) contains Searchtext
				put mynode.(SearchAttribute)
				insert mynode into mylist
			end if
		end if
		
		set nodeList to recFunc(myNode, searchtag,searchattribute,SearchText)
		insert nodeList into mylist
	end repeat
	
	return mylist
end recFunc

Console Output: will be the value found and the each node

42
[@"<name code=\"42\" displayName=\"New\"></name>"]

Unfortunately I am not able to share the associated XML. Let me know in case you have further questions on how to use it.

Cheers,
Karsten

Thanks for the reply, but I actually managed to find a solution using another of Dougs posts from a while ago utilising DocumentTreeFromXML (i was just focussed on the implimentation to update here):

set configFile to "C:\xml.config"
put file configFile into configXml	
put DocumentTreeFromXML(configXml) into g_ConfigSource
set nodeQuery to "/appSettings/add[@key='" & key & "']"	
put @value of node nodeQuery of g_ConfigSource into cfgValue

This is slightly out of context as i have this spread across 2 function calls and some parameterisation.

I also have to juggle the file content into the tree as the system does not seem to like dynamic path creation being used in the file access. The same can be said of the node query.

It is outside the scope of my use, but my XML is a unique key/value pair list so there are no duplicates. I don’t know if this approach would support a order / line item scenario where there are multiple nodes with the same key.