使用Applescript解析XML并基于XML属性引用标记块

使用Applescript解析XML并基于XML属性引用标记块,xml,applescript,Xml,Applescript,在Applescript中,是否有一种基于标记属性仅获取标记块的方法?例如,我只想获取与属性“style”关联的标记块。(我包括了一个缩写的xml。) 缩短的xml文件: 将XMLFile设置为“Macintosh HD:Users::Desktop:templateDescription.xml” 告诉应用程序“系统事件” 将toolbarControls设置为XML元素的XML元素XMLFile内容的XML元素1的“toolbars” 返回其XML元素“toolbarControls”的XM

在Applescript中,是否有一种基于标记属性仅获取标记块的方法?例如,我只想获取与属性“style”关联的标记块。(我包括了一个缩写的xml。)

缩短的xml文件: 将XMLFile设置为“Macintosh HD:Users::Desktop:templateDescription.xml” 告诉应用程序“系统事件” 将toolbarControls设置为XML元素的XML元素XMLFile内容的XML元素1的“toolbars” 返回其XML元素“toolbarControls”的XML属性“style”的值为“textToolbar”的每个toolbarControls项XMLFile内容元素1的XML元素“toolbars”的值为“textToolbar” 结束语
*新的复杂性:所以,我刚刚意识到一些xml使用注释,并且系统事件中存在一个已知的错误,当存在注释时,它无法正确解析xml。有办法解决这个问题吗?

使用循环来获取标记块

像这样:

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"
将XMLFile设置为“Macintosh HD:Users::Desktop:templateDescription.xml”
将toolbarControls_textToolbar设置为{}
告诉应用程序“系统事件”
将toolbarControls设置为XML元素的XML元素XML文件内容的XML元素1的“toolbars”
在工具栏控件中使用xmlElem重复此操作
尝试
告诉xmlElem,若XML属性“style”的值为“textToolbar”,则将toolbarControls\u textToolbar的末尾设置为contents
结束尝试
结束重复
结束语
return toolbarControls\u textToolbar——返回XML属性“style”的值为“textToolbar”的每个XML元素
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"