“Applescript XML”;无效索引“;带CDATA

“Applescript XML”;无效索引“;带CDATA,xml,applescript,Xml,Applescript,使用以下脚本分析以下XML会引发“无效索引”错误。我把它归结为CDATA没有包装在标记中这一事实。脚本似乎在初始读取中将CDATA计数为有效元素,但在后续读取中不使用它,因此丢弃索引并选择错误的元素 我怎样才能避开这件事 example.xml: <?xml version="1.0"?> <library> <info>Some info</info> <![CDATA[Yo]]> <books> &l

使用以下脚本分析以下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 ref_books to a reference to (XML elements of XML element "books" of XML element "library" whose name is "book")
        repeat with theBook in ref_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
tell application "System Events"
    tell XML file "~/Downloads/example.xml""
        tell (XML elements of XML element "books" of XML element "library" whose name is "book")
            repeat with theBook in it
                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
end tell

或者使用which子句上的
告诉
块,如下所示:

tell application "System Events"
    tell XML file "~/Downloads/example.xml"
        set ref_books to a reference to (XML elements of XML element "books" of XML element "library" whose name is "book")
        repeat with theBook in ref_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
tell application "System Events"
    tell XML file "~/Downloads/example.xml""
        tell (XML elements of XML element "books" of XML element "library" whose name is "book")
            repeat with theBook in it
                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
end tell