在Applescript中,有没有办法只根据tag属性获取标记块?例如,我只想抓取具有与之关联的属性“style”的标记块。 (我包括一个缩短的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
*新的复杂性:所以,我刚刚意识到有些xmls使用注释,并且系统事件存在一个已知错误,当有注释时它无法正确解析xml。有没有办法解决这个问题?
答案 0 :(得分:0)
使用循环来抓取标记块。
像这样:
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"