Reputation: 43
In Applescript, is there a way to only grab tag blocks based on tag attribute? For example, I only want to grab the tag blocks that have the attribute 'style' associated with them. (I included a shortened xml.)
shortened xml file:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<templateDescription>
<toolbars>
<toolbarControls style="textToolbar">
</toolbarControls>
<toolbarControls style="textToolbar">
</toolbarControls>
<toolbarControls definition="image">
</toolbarControls>
</toolbars>
</templateDescription>
set XMLFile to "Macintosh HD:Users:<user>:Desktop:templateDescription.xml"
tell application "System Events"
set toolbarControls to XML elements of XML element "toolbars" of XML element 1 of contents of XMLFile
return every item of toolbarControls whose value of XML attribute "style" of XML element "toolbarControls" of XML element "toolbars" of XML element 1 of contents of XMLFile is "textToolbar"
end tell
*new complication: So, I just realized that some xmls use comments and there's a known bug with system events where it can't correctly parse xml when there are comments. Is there a way to address this?
Upvotes: 1
Views: 287
Reputation: 7191
Use a loop to grab tag blocks.
Like this:
set XMLFile to "Macintosh HD:Users:<user>:Desktop:templateDescription.xml"
set toolbarControls_textToolbar to {}
tell application "System Events"
set toolbarControls to XML elements of XML element "toolbars" of XML element 1 of contents of XML file XMLFile
repeat with xmlElem in toolbarControls
try
tell xmlElem to if value of XML attribute "style" is "textToolbar" then set end of toolbarControls_textToolbar to contents
end try
end repeat
end tell
return toolbarControls_textToolbar -- return every XML element whose value of XML attribute "style" is "textToolbar"
Upvotes: 1