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.