Parsing xml w. XML-tools. Help needed w. Attribute-function.

Hoi,

I’m fairly new to applescript, so maybe my question below is really stupid, and the answer should be obvious, but hey… it’s crackin’ my brain!! Anyway, here it is:

In an application still in development i’m using XML-Tools from Latenightsw. I use it in an AS-app to parse some xml-files generated by a web-interface.

I have a problem with one of the functions supplied by LNSW. This function gets a XML-element with an attribute ‘name’ with a value ‘theName’. This works fine.
So, if i change |name| to |type| (use pipes so one can use applescript reserved words, correct?), it also works perfectly


on getNamedElement(theXML, theElementName, theName)
	-- find and return the first element with a particular name attribute
	
	if class of theXML is XML element or class of theXML is XML document then
		repeat with anElement in XML contents of theXML
			try
				if class of anElement is XML element and ¬
					XML tag of anElement is theElementName and ¬
					|name| of XML attributes of anElement is theName then
					return contents of anElement -- check the |name| thing
				end if
			on error number -1728
				-- ignore this error
			end try
		end repeat
	else if class of theXML is list then
		repeat with anElement in theXML
			try
				if class of anElement is XML element and ¬
					XML tag of anElement is theElementName and ¬
					|name| of XML attributes of anElement is theName then
					return contents of anElement -- check the |name| thing
				end if
			on error number -1728
				-- ignore this error
			end try
		end repeat
	end if
	
	return missing value
end getNamedElement

Next step was modifing the original function so it can accept a specified attribute like so: (notice ‘theAttribute’ input to the function)


on getElementByAttribute(theXML, theElementName, theAttribute, theName)
	-- find and return the first element with a particular name attribute
	--display dialog (theAttribute)
	--set yo to theAttribute as Unicode text
	if class of theXML is XML element or class of theXML is XML document then
		repeat with anElement in XML contents of theXML
			try
				if class of anElement is XML element and ¬
					XML tag of anElement is theElementName and ¬
					theAttribute of XML attributes of anElement is theName then
					return contents of anElement  -- check the theAttribute thing
				end if
			on error number -1728
				-- ignore this error
			end try
		end repeat
	else if class of theXML is list then
		repeat with anElement in theXML
			try
				if class of anElement is XML element and ¬
					XML tag of anElement is theElementName and ¬
					theAttribute of XML attributes of anElement is theName then
					return contents of anElement -- check the theAttribute thing
				end if
			on error number -1728
				-- ignore this error
			end try
		end repeat
	end if
	
	return missing value
end getElementByAttribute

And a function call like this, which should get an action-element with the type-attribute set to generate:


set action to getElementByAttribute(theXML, "action", "type", "generate")

But then… the fun stops. It doesn’t work. Calling the function like this returns missing value.

My best guess is that theAttribute must not be passed as a string, but something else, like an object or so. I can’t figure this one out. :rolleyes:

Ow: one last thing, XML attributes is a list type:


{type:"type",attr:"attribute",other:"other attribute"}

Can somebody help me to get this function going?

Thanx in advance,
Martijn.

Hello,

I found this XML-library for AS supporting X-Path which makes my life a whole lot easier. It can do a lot more than that as well. So everyone involved in parsing xml, check it out.

So, i’m gonna use this one instead… no need for the function above anymore. Thanx for all the replies :stuck_out_tongue:

Regards,
Martijn.

ps: i’m still curious to find the answer to my question…

The problem is that AppleScript does not allow you to access properties of records dynamically. The property name must be defiend at compile-time.
To get around this, you can use my List & Record Tools scripting addition which provides this command.
http://www.latenightsw.com/freeware/RecordTools/index.html