C
HAPTER
13: XML Rules
XML Rules Examples 210 end tell global myReturnString set myReturnString to "OK" tell application "Adobe InDesign CS5" try tell myXMLElement duplicate end tell on error myError set myReturnString to myError end try end tell return true end apply end script end DuplicateElement
XML rules and XML attributes
The following XML rule adds attributes to XML elements based on the content of their “name” element.
When you need to find an element by its text contents, copying or moving XML element contents to XML attributes attached to their parent XML element can be very useful in XML-rule scripting. While the subset of XPath supported by XML rules cannot search the text of an element, it can find elements by a specified attribute value. For the complete script, see AddAttribute.
global myGlueCode on run tell application "Adobe InDesign CS5" set myRootXML to first XML element of document 1 set myApplicationPath to file path set myFilePath to file path as string set myFilePath to myFilePath & "Scripts:Xml rules:glue code.scpt" set myGlueCode to load script file myFilePath set myRuleSet to {my AddAttribute()} end tell tell myGlueCode
__processRuleSet(myRootXML, myRuleSet, {}) end tell end run to AddAttribute() script AddAttribute property name : "AddAttribute" property xpath : "/devices/device/part_number"
--Adds the content of an XML element to an attribute of
--the parent of the XML element. This can make finding the
--element by its contnet much easier and faster.
on apply(myXMLElement, myRuleProcessor) global myReturnString set myReturnString to "OK" tell application "Adobe InDesign CS5" try
C
HAPTER
13: XML Rules
XML Rules Examples 211 tell myXMLElement set myString to contents of text 1 tell parent make XML attribute with properties {name:"part_number", value:myString} end tell end tell on error myError set myReturnString to myError end try end tell return true end apply end script end AddAttribute
In the previous XML rule, we copied the data from an XML element into an XML attribute attached to its parent XML element. Instead, what if we want to move the XML element data into an attribute and remove the XML element itself? Use the convertToAttribute
method, as shown in the following script (from the
ConvertToAttribute tutorial script): global myGlueCode on run tell application "Adobe InDesign CS5" set myRootXML to first XML element of document 1 set myApplicationPath to file path set myFilePath to file path as string set myFilePath to myFilePath & "Scripts:Xml rules:glue code.scpt" set myGlueCode to load script file myFilePath set myRuleSet to {my ConvertToAttribute()} end tell tell myGlueCode
__processRuleSet(myRootXML, myRuleSet, {}) end tell end run to ConvertToAttribute() script ConvertToAttribute property name : "ConvertToAttribute" property xpath : "/devices/device/part_number" on apply(myXMLElement, myRuleProcessor) tell myGlueCode
__skipChildren(myRuleProcessor) end tell global myReturnString set myReturnString to "OK" tell application "Adobe InDesign CS5" try tell myXMLElement convert to attribute ("PartNumber") end tell on error myError set myReturnString to myError end try end tell return true end apply end script end ConvertToAttribute