Help with xml parsing please

I am trying to get the value of of this element “egGr:nrpages” from a xml file. I just can’t seem to do it. Any help would be appreciated.

<?xml version="1.0" encoding="UTF-8"?> <?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>

<x:xmpmeta xmlns:x=“adobe:ns:meta/” x:xmptk=“XMP Core 4.4.0”>
<rdf:RDF xmlns:rdf=“http://www.w3.org/1999/02/22-rdf-syntax-ns#”>
<rdf:Description rdf:about=“”
xmlns:egGr=“http://ns.esko-graphics.com/grinfo/1.0/
xmlns:egInk=“http://ns.esko-graphics.com/inkinfo/1.0/”>
egGr:inks
rdf:Seq
<rdf:li rdf:parseType=“Resource”>
egInk:namecyan</egInk:name>
egInk:typeprocess</egInk:type>
egInk:bookprocess</egInk:book>
egInk:egnamecyan</egInk:egname>
egInk:frequency150.</egInk:frequency>
egInk:angle15.</egInk:angle>
egInk:dotshape/
egInk:r0.</egInk:r>
egInk:g.6491146478</egInk:g>
egInk:b.9086514743</egInk:b>
egInk:attributenormal</egInk:attribute>
egInk:printingmethodunknown</egInk:printingmethod>
</rdf:li>
<rdf:li rdf:parseType=“Resource”>
egInk:namemagenta</egInk:name>
egInk:typeprocess</egInk:type>
egInk:bookprocess</egInk:book>
egInk:egnamemagenta</egInk:egname>
egInk:frequency150.</egInk:frequency>
egInk:angle75.</egInk:angle>
egInk:dotshape/
egInk:r.921078013</egInk:r>
egInk:g.1982994402</egInk:g>
egInk:b.5299102447</egInk:b>
egInk:attributenormal</egInk:attribute>
egInk:printingmethodunknown</egInk:printingmethod>
</rdf:li>
<rdf:li rdf:parseType=“Resource”>
egInk:nameyellow</egInk:name>
egInk:typeprocess</egInk:type>
egInk:bookprocess</egInk:book>
egInk:egnameyellow</egInk:egname>
egInk:frequency150.</egInk:frequency>
egInk:angle90.</egInk:angle>
egInk:dotshape/
egInk:r1.</egInk:r>
egInk:g.9642572</egInk:g>
egInk:b0.</egInk:b>
egInk:attributenormal</egInk:attribute>
egInk:printingmethodunknown</egInk:printingmethod>
</rdf:li>
<rdf:li rdf:parseType=“Resource”>
egInk:nameblack</egInk:name>
egInk:typeprocess</egInk:type>
egInk:bookprocess</egInk:book>
egInk:egnameblack</egInk:egname>
egInk:frequency150.</egInk:frequency>
egInk:angle45.</egInk:angle>
egInk:dotshape/
egInk:r.1870992878</egInk:r>
egInk:g.1778713106</egInk:g>
egInk:b.1984996809</egInk:b>
egInk:attributenormal</egInk:attribute>
egInk:printingmethodunknown</egInk:printingmethod>
</rdf:li>
<rdf:li rdf:parseType=“Resource”>
egInk:name286</egInk:name>
egInk:typepantone</egInk:type>
egInk:bookpms1000c</egInk:book>
egInk:egname286</egInk:egname>
egInk:frequency150.</egInk:frequency>
egInk:angle45.</egInk:angle>
egInk:dotshape/
egInk:r0.</egInk:r>
egInk:g.2115764123</egInk:g>
egInk:b.6512271668</egInk:b>
egInk:attributenormal</egInk:attribute>
egInk:printingmethodunknown</egInk:printingmethod>
</rdf:li>
<rdf:li rdf:parseType=“Resource”>
egInk:name292</egInk:name>
egInk:typepantone</egInk:type>
egInk:bookpms1000c</egInk:book>
egInk:egname292</egInk:egname>
egInk:frequency150.</egInk:frequency>
egInk:angle45.</egInk:angle>
egInk:dotshape/
egInk:r.4081936181</egInk:r>
egInk:g.7360990869</egInk:g>
egInk:b.9396798118</egInk:b>
egInk:attributenormal</egInk:attribute>
egInk:printingmethodunknown</egInk:printingmethod>
</rdf:li>
</rdf:Seq>
</egGr:inks>
egGr:nrpages3</egGr:nrpages>
egGr:startlogpage1</egGr:startlogpage>
egGr:unitsmm</egGr:units>

Hi,

try this, “path:to:file.xml” is the full HFS path to the xml file


tell application "System Events"
	set XMLFile to XML file "path:to:file.xml"
	set numberOfPages to value of XML element "egGr:nrpages" of XML element "rdf:Description" of XML element "rdf:RDF" of XML element 1 of contents of XMLFile
end tell


I tested stefanK script with :

set fichier_file to (path to desktop as text) & "file.xml"
tell application "System Events"
	set XMLFile to XML file fichier_file
	set numberOfPages to value of XML element "egGr:nrpages" of XML element "rdf:Description" of XML element "rdf:RDF" of XML element 1 of contents of XMLFile
end tell

and got :
→ error number -1728 from XML element “rdf:RDF” of XML element 1 of XML file “Macintosh HD:Users:yvankoenig:Desktop:file.xml”

So I decided to use brute force :

set fichier_file to (path to desktop as text) & "file.xml"

read file fichier_file
item 2 of my decoupe(result, "<egGr:nrpages>")
item 1 of my decoupe(result, "<")
--> "3"

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

and got the correct result “3”

Yvan KOENIG (VALLAURIS, France) jeudi 12 décembre 2013 11:05:25

Did you add the missing closing tags in the xml text at the end ?

. </rdf:Description> </rdf:RDF> </x:xmpmeta>

you need to add the following to the end of the XML (the xml in the initial post is incomplete):

</rdf:Description> </rdf:RDF> </x:xmpmeta>
Then Stefan’s post will work properly.

edit: I just see Stefan’s post after submitting mine.

The problem was not the forgotten lines which I added.

It was that after copying from the original mail and pasting in TextEdit, many space characters were embedded as “C2 A0”. Opening the file with TextWrangler displayed the file correctly indented but when I saved, the surprising characters weren’t replaced by standard spaces and the script was fooled. I used Hexedit to replace the “C2 A0” by “20” and now the Stefan’s script behaves flawlessly.

Just a request to Stefen and David : may you correct the typo which I made in my original message ? A letter s is wrongly replacing the wanted d. This way everybody would read « tested » as it was supposed to do.

Yvan KOENIG (VALLAURIS, France) jeudi 12 décembre 2013 14:58:26

Glad you found your problem. Weird that you system replaces all spaces by non-braking spaces when you paste. At least it’s not default system behavior as it for text edit (maybe a browser issue). Just to be clear, the error you mentioned in post #3 is just because you copied and pasted the code from TS the wrong way!?

Would be glad to know what I made wrongly.

I entered MacScripter with Safari
Copied from the thread
Pasted in a TextEdit window in text format.
Saved as File.txt
Then renamed as File.xml

Yvan KOENIG (VALLAURIS, France) jeudi 12 décembre 2013 18:23:50

Thanks a lot StafanK. Worked great