AutoHotkey Community

It is currently May 24th, 2012, 8:56 pm

All times are UTC [ DST ]




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 417 posts ]  Go to page 1, 2, 3, 4, 5 ... 28  Next
Author Message
PostPosted: March 17th, 2007, 9:28 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
A simple and easy set of functions for parsing XML content with xpath including save and load routines. Extremely fast and lightweight for AutoHotkey; nodes and attributes can be created and removed directly within your expressions without DOM traversal.

The included manual covers some of the common uses of xpath and demonstrates how to use this library. Unlike my other scripts this one is commented from the source for anyone who wants to know how it works.

Download

I'd like to thank everyone who posted suggestions, bug reports and advice.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Last edited by polyethene on August 27th, 2009, 11:11 am, edited 6 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2007, 9:47 pm 
Wow! :) I'm baffled. Great work!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2007, 11:35 am 
Offline

Joined: October 10th, 2005, 10:44 am
Posts: 299
Location: Germany
That's just awesome, Titan!
Seriously impressive!

_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2007, 10:45 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1933
Location: Germany
Wow thx I was waiting for this since I know you was working on it. 8)

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2007, 9:43 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
/applaud

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 23rd, 2007, 3:12 pm 
Offline

Joined: October 10th, 2005, 10:44 am
Posts: 299
Location: Germany
Hmm ... I can't seem to get this working - either I'm stupid, or it's a bug:

I'm trying to retrieve the latest link from the xkcd feed (see below for a snapshot of its current state).
For that I use the following code:
Code:
xkcdFeed = http://xkcd.com/rss.xml
rss := XmlDoc(xkcdFeed)
latest := XPath(rss, "/rss/channel/item[1]/link")
(not sure whether the nodes array is zero-based or one-based, but that doesn't matter here)

However, this returns the full ITEM node of the last (third) item:
Code:
<item>

 <title>Keyboards are Disgusting
  </title>

 <link>http://xkcd.com/c237.html
  </link>

 <description>&lt;img src="http://imgs.xkcd.com/comics/keyboards_are_disgusting.png" title="Alternate method: convince them to pretend it's an Etch-a-Sketch and try to erase it." alt="Alternate method: convince them to pretend it's an Etch-a-Sketch and try to erase it." />
 </description>

<guid isPermaLink="true">http://xkcd.com/c237.html
 </guid>

<pubDate>2007-03-19
 </pubDate>

</item>

I can't explain this - can anyone else?



Current contents of the xkcd feed (for reference):
Code:
<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">

<channel>
<title>xkcd.com</title>
<link>http://www.xkcd.com</link>
<description>xkcd.com: A webcomic of romance and math humor.</description>
<language>en</language>
<copyright>Copyright 2005-2006 Randall Munroe</copyright>
<pubDate>Fri, 23 Mar 2007 07:47:44 -0400</pubDate>
<lastBuildDate>Fri, 23 Mar 2007 07:47:44 -0400</lastBuildDate>
<managingEditor>rmunroe@gmail.com</managingEditor>
<webMaster>davean@sciesnet.net</webMaster>

<item>
<title>Blagofaire</title>
<link>http://xkcd.com/c239.html</link>
<description>&lt;img src="http://imgs.xkcd.com/comics/blagofaire.png" title="Things were better before the Structuring and the Levels." alt="Things were better before the Structuring and the Levels." /></description>
<guid isPermaLink="true">http://xkcd.com/c239.html</guid>
<pubDate>2007-03-23</pubDate>
</item>

<item>
<title>Pet Peeve #114</title>
<link>http://xkcd.com/c238.html</link>
<description>&lt;img src="http://imgs.xkcd.com/comics/pet_peeve_114.png" title="I'm reading a book, thank you very much." alt="I'm reading a book, thank you very much." /></description>
<guid isPermaLink="true">http://xkcd.com/c238.html</guid>
<pubDate>2007-03-21</pubDate>
</item>

<item>
<title>Keyboards are Disgusting</title>
<link>http://xkcd.com/c237.html</link>
<description>&lt;img src="http://imgs.xkcd.com/comics/keyboards_are_disgusting.png" title="Alternate method: convince them to pretend it's an Etch-a-Sketch and try to erase it." alt="Alternate method: convince them to pretend it's an Etch-a-Sketch and try to erase it." /></description>
<guid isPermaLink="true">http://xkcd.com/c237.html</guid>
<pubDate>2007-03-19</pubDate>
</item>

</channel>
</rss>

_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 23rd, 2007, 8:19 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
The following gives me the correct result:

Code:
xkcdFeed = http://xkcd.com/rss.xml
rss := XmlDoc(xkcdFeed)
latest := XPath(rss, "/rss/channel/item[1]/link/text()")
MsgBox, %latest%

Try re-downloading XPath.ahk if it doesn't work for you. Note that the function preserves whitespace regardless of PI, so you can use the ^\s*|\s*$ regex to convert the string into a valid URI.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 24th, 2007, 8:57 am 
Offline

Joined: October 10th, 2005, 10:44 am
Posts: 299
Location: Germany
Thanks Titan; I found the problem:

I had opened XPath.ahk in the browser (Firefox) and just copied the code into my file. For whatever reason, that seems to create problems; it works just fine if I actually download the file and take the code from there.

_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 24th, 2007, 7:38 pm 
Correct the link on this page - http://www.autohotkey.net/~Titan/xpath.html


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 24th, 2007, 7:47 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
Thanks, sorry about that.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 24th, 2007, 11:26 pm 
No problem 8)

btw, very nice script. It will help me a lot.


Report this post
Top
  
Reply with quote  
PostPosted: April 4th, 2007, 11:13 am 
Offline

Joined: April 4th, 2007, 10:23 am
Posts: 6
I tried the code
Code:
xkcdFeed = http://xkcd.com/rss.xml
rss := XmlDoc(xkcdFeed)
latest := XPath(rss, "/rss/channel/item[1]/link/text()")
MsgBox, %latest%

but I got some extra tailing bytes of 0xA0.
any idea?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 4th, 2007, 1:13 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
http://www.xkcd.com/rss.xml is not very well-formed, it has unescaped closing tags within /rss/channel/description. I'll improve the parser to ignore such closing tags without opening ones and strip off generated whitespace (&nbsp;/0xA0) for the next version. In the meantime you could use:

Code:
xkcdFeed = http://www.xkcd.com/rss.xml ; URI
xkcdLocFeed = %A_Temp%\xkcd.xml ; local path
UrlDownloadToFile, %xkcdFeed%, %xkcdLocFeed%
FileRead, rss, %xkcdLocFeed%
StringReplace, rss, rss, /></description>, /&gt;</description>, All ; escape closing tags
rss := XmlDoc(rss) ; load as var ...
latest := XPath(rss, "/rss/channel/item[1]/link/text()")
MsgBox, %latest%


Thanks for the feedback.

Edit: now fixed in version 1.01 thanks.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 9th, 2007, 4:38 am 
Offline

Joined: April 4th, 2007, 10:23 am
Posts: 6
I tried version 1.01 and it works just fine.
Thanks a lot! :lol:


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Operators ">" and "="
PostPosted: April 10th, 2007, 1:53 pm 
Offline

Joined: May 27th, 2005, 4:14 pm
Posts: 35
Hallo, 1st of all - THIS IS JUST WHAT I NEEDED, thanks.

Nevertheless I don't understand the follwing: Why ist Book2 with a price of 2.0 found by [price>2.0] and it's not found by [price=2.0]?
Code:
#Include XPath.ahk
xPath(t, "/bookstore[+1]")
loop, 5 {
    xPath(t, "/bookstore/book[+1]/title[+1]", "title" A_index)
    xPath(t, "/bookstore/book[" A_index "]/price[+1]", A_index ".0")
}
E := "[price>=2.0] " XPath(t, "/bookstore/book[price>=2.0]/title/text()")
E := E "`n[price>2.0] " XPath(t, "/bookstore/book[price>2.0]/title/text()") " -> also Book2 found?"
E := E "`n[price=2.0] " XPath(t, "/bookstore/book[price=2.0]/title/text()") " -> not found?"
E := E "`n[price=2] " XPath(t, "/bookstore/book[price=2]/title/text()") " -> not found?"
E := E "`n---" t
Gui, Add, Edit, w400 h400 ReadOnly -Wrap, %E%
Gui, Show, , xPath


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 417 posts ]  Go to page 1, 2, 3, 4, 5 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Cerberus, Uberi and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group