• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
XML attributes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

XML attributes


  • Subject: XML attributes
  • From: Jörgen Stahle <email@hidden>
  • Date: Sat, 24 Nov 2018 12:12:31 +0100

Hi, there!


Some years ago Shane Stanley helped me with creating script libraries to
convert XML to Applescript record and vice versa – which I appreciated very
much.


These libraries can work as replacements for XML Tools.osax – and the need
for that in our production has become urgent now, since the use of third
party scripting additions is blocked in MacOS 10.14 Mojave.


They works very well - except for one critical issue concerning attributes
when exporting a record (representing XML) to XML:

It can’t add children WITH attributes, only add attributes to existing
parent elements. This means I can never assign attributes to elements at
the lowest level in the parent-child hierarchy (or at least I don’t know
how to write the record to accomplish that).


So with the current version of my script library ”record_to_XML” (code
below) I cannot make the following xml.  An attribute can be assigned to
the element ’partner approach’ since it contains children, but not to the
nested elements in ’partner approach’, as ’city’

<character>

    <firstName>Saga</firstName>

    <lastName>Norén</lastName>

    <city country="Sweden">Malmö</city> <!-- Attribute can't be made -->

    <partner approach=\"dogged\"> <!-- OK -->

        <firstName>Martin</firstName>

        <lastName>Rohde</lastName>

        <city country="Denmark">København</city> <!-- Attribute can't be
made -->

    </partner>

</character>"


Here is the code of  ”record_to_XML.scptd”

*use* *framework* "Foundation"


*on* makeXMLDocWithRecord:theRecord withRoot:rootName

-- make root element

*set* rootElement *to* *current application's* NSXMLNode's elementWithName:
rootName

-- make XML document

*set* theXMLDocument *to* *current application's* NSXMLDocument's alloc()'s
initWithRootElement:rootElement

theXMLDocument's setDocumentContentKind:(*current application's*
NSXMLDocumentXMLKind)

theXMLDocument's setStandalone:*true*

theXMLDocument's setCharacterEncoding:"UTF-8"

-- make dictionary from record

*set* anNSDictionary *to* *current application's* NSDictionary's
dictionaryWithDictionary:theRecord

-- add children to root element

*its* addChildOrAttributesTo:rootElement withDictionary:anNSDictionary

-- return as string with whatever formatting options you want

*return* (theXMLDocument's XMLStringWithOptions:(*current application's*
NSXMLNodePrettyPrint)) *as* *text*

*end* makeXMLDocWithRecord:withRoot:


*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*

*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*

(newElement's setObjectValue:theValue)

*end* *if*

*end* *if*

*end* *repeat*

*end* addChildOrAttributesTo:withDictionary:



So the following record can be processed with desired result…


*set* theRecord *to* {firstName:"Saga", lastName:"Norén", city:"Malmö",
partner:{firstName:"Martin", lastName:"Rohde", city:"København", attributes
:{approach:"dogged"}}}

*its* makeXMLDocWithRecord:theRecord withRoot:"character"


… but there is no way to add attributes to  ’firstName’, ’lastName’ or
’city’.



Since I lack knowledge of ObjC i fail to fix this myself. But it seems
addChildOrAttributesTo
needs to be replaces by a addChild_withAttributesIfDefined (and some
structural change be made)

What I understand is that the format of the record input must be changed to
something like this:


*set* theRecord *to* {firstName:{|contents|:"Saga"},
lastName:{|contents|:"Norén"},
city:{|contents|:"Malmö", attributes:{country:"Sweden"}}, partner:{
attributes:{approach:"dogged"}, firstName:{|contents|:"Martin"}, lastName:{
|contents|:"Rohde"}, city:{|contents|:"København", attributes:{country
:"Denmark"}}}}


So… I wonder if Shane or anyone else could help me with a fix for that… It
would be very much appreciated.
 _______________________________________________
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

  • Follow-Ups:
    • Re: XML attributes
      • From: Bob Stern <email@hidden>
    • Re: XML attributes
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Re: Where is macOS 10.14 AppleScript Release Notes?
  • Next by Date: Re: XML attributes
  • Previous by thread: Re: Where is macOS 10.14 AppleScript Release Notes?
  • Next by thread: Re: XML attributes
  • Index(es):
    • Date
    • Thread