【发布时间】:2017-08-27 15:50:13
【问题描述】:
使用以下脚本解析以下 XML 会引发“无效索引”错误。我已将其隔离为CDATA 未包含在标签内的事实。似乎该脚本在初始读取中将CDATA 计为有效元素,但在后续读取中不使用它,因此丢弃了索引并选择了错误的元素。
我该如何解决这个问题?
example.xml:
<?xml version="1.0"?>
<library>
<info>Some info</info>
<![CDATA[Yo]]>
<books>
<book country="US">
<name>The Secret Lives of Cats</name>
<publisher>Feline Press</publisher>
</book>
</books>
</library>
脚本:
tell application "System Events"
tell XML file "~/Downloads/example.xml"
set books to every XML element of XML element "books" of XML element "library" whose name is "book"
repeat with a from 1 to length of books
set theBook to item a of books
tell theBook
name of every XML element
name of every XML attribute
value of every XML attribute
end tell
end repeat
end tell
end tell
如果您删除 <![CDATA[Yo]]> 部分并运行脚本,您会看到它按预期工作。
【问题讨论】:
标签: xml applescript