2017-04-02 4 views
0

다음 스크립트로 다음 XML을 구문 분석하면 "잘못된 색인"오류가 발생합니다. 나는 CDATA이 태그 안에 싸여 있지 않다는 사실을 알아 냈습니다. 스크립트는 CDATA을 초기 읽기에서 유효한 요소로 계산하지만 후속 읽기에서는 사용하지 않으므로 색인을 버리고 잘못된 요소를 선택합니다.Applescript XML "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> 

스크립트 : 당신이 <![CDATA[Yo]]> 부분을 제거하고 스크립트를 실행하는 경우

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 

은 참고 예 https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/WorkwithXML.html

의 부분적 수정 된 버전입니다 , 예상대로 작동하는 것을 볼 수 있습니다.

답변

0

사용과 같은 참조 된 개체 :

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 블록을 사용

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