Re: XML attributes
Re: XML attributes
- Subject: Re: XML attributes
- From: Shane Stanley <email@hidden>
- Date: Tue, 27 Nov 2018 22:30:14 +1100
On 27 Nov 2018, at 7:03 pm, Jörgen Stahle <email@hidden> wrote:
>
> But i CAN NOT create this one with multiple "page" sub elements
> <example>
> <format date="20181126" product="NYH">
> <pages subProduct="105">
> <page id="243" template="template1.indd"></page>
> <page id="244" template="template2.indd"></page>
> </pages>
> </format>
> </example>
You can modify the handler something like this, using "elements" as a label for
a list of elements:
on addChildOrAttributesTo:parentElement withDictionary:theDict
set theKeys to theDict's allKeys() as list
repeat with i from 1 to count of theKeys
set theKey to item i of theKeys
set theValue to (theDict's objectForKey:theKey)
if theKey as text = "attributes" then
(parentElement's setAttributesWithDictionary:theValue)
else if theKey as text = "value_and_attributes" then
set newDict to (theDict's objectForKey:theKey)
set newName to (newDict's objectForKey:"value")
set theAtts to (newDict's objectForKey:"attributes")
(parentElement's setAttributesWithDictionary:theAtts)
(parentElement's setObjectValue:newName)
else if theKey as text = "elements" then
set theArray to (theDict's objectForKey:theKey)
repeat with anElement in theArray
(its addChildOrAttributesTo:parentElement
withDictionary:anElement)
end repeat
else
set newElement to (current application's NSXMLNode's
elementWithName:theKey)
(parentElement's addChild:newElement)
if (theValue's isKindOfClass:(current application's
NSDictionary)) as boolean then
(its addChildOrAttributesTo:newElement
withDictionary:theValue)
else if (theValue's isKindOfClass:(current
application's NSArray)) as boolean then
repeat with aValue in theValue
(its addChildOrAttributesTo:newElement
withDictionary:aValue)
end repeat
else
(newElement's setObjectValue:theValue)
end if
end if
end repeat
end addChildOrAttributesTo:withDictionary:
set theRecord to {format:{attributes:{|date|:"20181126", product:"NYH"},
pages:{attributes:{subProduct:"105"}, elements:{{page:{attributes:{|id|:"243",
template:"template1.indd"}}}, {page:{attributes:{|id|:"242",
template:"template2.indd"}}}}}}}
its makeXMLDocWithRecord:theRecord withRoot:"example"
But fundamentally I think at this stage you're going about it the long way.
You're having to first build complex record structures, when really you could
be building the XML directly instead.
I suspect you'd be better to call the main handler that builds the initial
document, then call a variation on the second each time you would otherwise be
adding another level to your record. The second handler could then be
simplified to something like:
on addChildTo:parentElement withName:theName withValue:theValue
andAttributes:attRecord
set newElement to (current application's NSXMLNode's
elementWithName:theName)
(parentElement's addChild:newElement)
if theValue is not missing value then newElement's
setObjectValue:theValue
if attRecord is not missing value then newElement's
setAttributesWithDictionary:attRecord
return newElement
end addChildTo:withName:withValue:andAttributes:
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden