PhantomScript v1.7 Reference - Copyright © 2006 P1 Systems Incorporated. All Rights Reserved.

Prev | Next | Users Guide | PhantomScript Reference | P1 Systems


XML

XMLMatch

Syntax

XMLMatch <xpath> <xml> <nslist>

Description

Return a list of XML nodes matching the provided XPath.

Parameters

<xpath>

The XPath expression that will be used to select the nodes returned

<xml>

The XML document to which the XPath expression will be applied.

<nslist>

A list of name=value pairs in which the name is the name used for the namespace in the XPath expression (i.e. the namespace identifier) and the value is the actual namespace. (Example below.)

Remarks

The most common reason why XPath queries don't return expected results is not using namespace identifiers where required.

The PhantomScript XML-handling statements are XMLMatch, XMLSelect, XMLSelectText, and XMLTransform.

Example

Init:
SetPhantom Normal LoggingOn ErrorPopupsOn ContinueOnError
SetStringEscapeCharacter ~
CapsLockOff
NumLockOff
ScrollLockOff
Return
Start::
Local AmazonURL = "http://webservices.amazon.com/onca/xml"

! The AmazonECS web service request arguments
Local args
InsertKeyedValue args "Service" "AWSECommerceService"
InsertKeyedValue args "Version" "2005-03-23"
InsertKeyedValue args "Operation" "ItemSearch"
InsertKeyedValue args "ContentType" "text/xml"
InsertKeyedValue args "SubscriptionId" "[Your Access ID]"
InsertKeyedValue args "Validate" "False"
InsertKeyedValue args "XMLEscaping" "Double"
InsertKeyedValue args "SearchIndex" "Books"
InsertKeyedValue args "Author" "Neal Stephenson"
InsertKeyedValue args "Title" "Snow Crash"
InsertKeyedValue args "ResponseGroup" "Reviews"

PrepareHTTPGetRequest
RequestURL=AmazonURL
RequestArgs=args
Result=request

! The formatted request URL looks like this:
! http://webservices.amazon.com/onca/xml?Title=Snow%20Crash&ContentType=text%2fxml"
! &ResponseGroup=Reviews&XMLEscaping=Double&Service=AWSECommerceService&SearchIndex"
! =Books&Author=Neal%20Stephenson&Version=2005-03-23&Operation=ItemSearch&Validate=
! False&SubscriptionId=[Your Access ID]

InternetGet URL=request Result=buf

Local ns = "ns=http://webservices.amazon.com/AWSECommerceService/2005-03-23"

reviews = XMLMatch "//ns:Review" buf ns

Local txt = ""

ResetList reviews
While MoreItems reviews Loop
review = NextItem reviews
dt = XMLSelectText "//ns:Date" review ns
summary = XMLSelectText "//ns:Summary" review ns
txt = txt & dt & "~n" & summary & "~n~n"
EndLoop

MessageBox Text=txt

ExitScript

!
! PrepareHTTPGetRequest - Prepare a GET request URL for InternetGet
!
! RequestURL - The URL to which the request will be sent
! RequestArgs - A KeyedValueList of request argument names and values
!
! Returns - The prepared request URL ready for use with InternetGet
!
Procedure PrepareHTTPGetRequest ArgList=RequestURL,RequestArgs
Local request
Local k
Local argkeys

argkeys = KeyList RequestArgs
k = Item1 argkeys
request = RequestURL & "?" & k & "=" & URLEncode FindKeyedValue
RequestArgs k
While MoreItems argkeys Loop
k = NextItem argkeys
request = request & "&" & k & "=" & URLEncode FindKeyedValue
RequestArgs k
EndLoop

SetResultValue request
EndProcedure

The above complete script performs a search using Amazon's ECommerce service for the book Snow Crash by Neal Stephenson. The review summaries contained in the result are displayed in the message box. Note the use of the Amazon webservices namespace in the XPath expressions. (To execute the request to Amazon you need an Amazon Access ID, which is free. You can obtain one at amazonaws.com.) The script output looks like this:

Example

 2006-04-25
Ranks just ahead of Neuromancer in my all time list

2006-04-07
Enjoyable, but not as much as hyped

2006-03-29
One of the best in its genre.

2006-03-23
Here comes the Metaverse!

2006-03-22
Just Read It

2006-06-03
Great Book.

Here is the (formatted) XML that was processed by the above script to produce the results shown:

Example

<?xml version="1.0" encoding="UTF-8"?>
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-03-23">
<OperationRequest>
<HTTPHeaders>
<Header Name="UserAgent" Value="Phantom for Windows"/>
</HTTPHeaders>
<RequestId>0MZTRPMN6AKS7P3WR572</RequestId>
<Arguments>
<Argument Name="Service" Value="AWSECommerceService"/>
<Argument Name="Title" Value="Snow Crash"/>
<Argument Name="ContentType" Value="text/xml"/>
<Argument Name="Validate" Value="False"/>
<Argument Name="SearchIndex" Value="Books"/>
<Argument Name="XMLEscaping" Value="Double"/>
<Argument Name="Author" Value="Neal Stephenson"/>
<Argument Name="SubscriptionId" Value="[Your Amazon Access ID appears here]"/>
<Argument Name="Version" Value="2005-03-23"/>
<Argument Name="ResponseGroup" Value="Reviews"/>
<Argument Name="Operation" Value="ItemSearch"/>
</Arguments>
<RequestProcessingTime>0.206147909164429</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
<IsValid>True</IsValid>
<ItemSearchRequest>
<Author>Neal Stephenson</Author>
<ResponseGroup>Reviews</ResponseGroup>
<SearchIndex>Books</SearchIndex>
<Title>Snow Crash</Title>
</ItemSearchRequest>
</Request>
<TotalResults>3</TotalResults>
<TotalPages>1</TotalPages>
<Item>
<ASIN>0553380958</ASIN>
<CustomerReviews>
<AverageRating>4.0</AverageRating>
<TotalReviews>464</TotalReviews>
<TotalReviewPages>93</TotalReviewPages>
<Review>
<ASIN>0553380958</ASIN>
<Rating>5</Rating>
<HelpfulVotes>2</HelpfulVotes>
<CustomerId>A3LJIPKG2N204</CustomerId>
<TotalVotes>3</TotalVotes>
<Date>2006-04-25</Date>
<Summary>Ranks just ahead of Neuromancer in my all time list</Summary>
<Content>This is my all time favorite in the "cyberpunk" genre... if you like William Gibson you'll love Neil Stephenson... Stephenson offers the same great "feel" but with a faster pace and more enjoyable tone. Now I just want to see a Snow Crash movie.</Content>
</Review>
<Review>
<ASIN>0553380958</ASIN>
<Rating>3</Rating>
<HelpfulVotes>2</HelpfulVotes>
<CustomerId>A20VZ3L7DLQXOL</CustomerId>
<TotalVotes>4</TotalVotes>
<Date>2006-04-07</Date>
<Summary>Enjoyable, but not as much as hyped</Summary>
<Content>I really did enjoy this book for the most part. I work in the high tech industry, this gives me a sort of geek license to enjoy the nerdy aspects of the book. The metaverse was a very interesting concept - and not as far off as it seems. The references and comparisons to Sumerian mythos/languages were some of the more interesting aspects of the store (along with the general geekiness of the metaverse)
&lt;br /&gt;
&lt;br /&gt;Why three stars instead of more? The characters were not nearly as fleshed out as they needed to be, the ending was quite anti-climactic, and the writing was in places quite sloppy. Funny character names such as "Hiro Protagonist" does not make Neal Stephenson Thomas Pynchon. i.e.:
&lt;br /&gt;
&lt;br /&gt;"If we get out of this, will you be my girl?"
&lt;br /&gt;
&lt;br /&gt;This is one of the many gag moments - along with the extremely clunky male/female relationships and a hamhanded delivery of a 'sex scene'. Luckily were as many 'wow' moments as there were "barf" moments. This is what kept me reading, and not dashing the book into the fireplace.</Content>
</Review>
<Review>
<ASIN>0553380958</ASIN>
<Rating>5</Rating>
<HelpfulVotes>1</HelpfulVotes>
<CustomerId>A1HNPTX8A101U8</CustomerId>
<TotalVotes>2</TotalVotes>
<Date>2006-03-29</Date>
<Summary>One of the best in its genre.</Summary>
<Content>This is one of those books that will make you want to read other Neil Stephenson pieces. That is what has happened to me. It contains very competent writing and very memorable images.</Content>
</Review>
<Review>
<ASIN>0553380958</ASIN>
<Rating>4</Rating>
<HelpfulVotes>1</HelpfulVotes>
<CustomerId>A19UMVGOLLR7SB</CustomerId>
<TotalVotes>5</TotalVotes>
<Date>2006-03-23</Date>
<Summary>Here comes the Metaverse!</Summary>
<Content>SNOW CRASH is set in the (not too distant) future, in a world where accessing the Internet has evolved from web pages like you are presently looking at to a 3D virtual world known as the Metaverse. Hiro Protagonist, the main character, is on of the most highly skilled Metaverse hackers. I thoroughly enjoyed the first 2/3 of the book, as it dealt with a lot of the use of the Metaverse, but it seemed as though Stephenson got away from that in the last couple of chapters, and the ending was a little flat for me.
&lt;br /&gt;
&lt;br /&gt;Want to get involved in something that is possibly a precursor to a real Metaverse? Visit www.secondlife.com. </Content>
</Review>
<Review>
<ASIN>0553380958</ASIN>
<Rating>5</Rating>
<HelpfulVotes>1</HelpfulVotes>
<CustomerId>A3HANBLGEL291D</CustomerId>
<TotalVotes>2</TotalVotes>
<Date>2006-03-22</Date>
<Summary>Just Read It</Summary>
<Content>Snow Crash is one of the best pieces of modern fiction I've ever read, and I can't recommend it highly enough.
&lt;br /&gt;
&lt;br /&gt;Great science fiction always succeeds when it says something about our own society and makes us view our world in a new way, and despite the fact that this book is about 15 years old, it remains as relevant as it's ever been (which can't be said for all pieces of modern sci-fi). Several of the events definitely provoke a "What if that really happened?" response, and perhaps the most provocative aspect of such plot points is that some are more possible now than they were when the book was written.
&lt;br /&gt;
&lt;br /&gt;The characters are all incredibly well developed, each with his/her own agenda and voice, and the plot is tightly constructed, with nary a wasted passage. Even the sections that read like amusing tangents end up having a purpose in advancing the story.
&lt;br /&gt;
&lt;br /&gt;And as for plot -- it's great. In some ways it had me thinking that it was a bit of a stretch, but the explanations are made as thoroughly as possible to allow a suitable level of suspension of disbelief.
&lt;br /&gt;
&lt;br /&gt;Seriously -- just read it.</Content>
</Review>
</CustomerReviews>
</Item>
<Item>
<ASIN>344245302X</ASIN>
<CustomerReviews>
<AverageRating>5.0</AverageRating>
<TotalReviews>1</TotalReviews>
<TotalReviewPages>1</TotalReviewPages>
<Review>
<ASIN>344245302X</ASIN>
<Rating>5</Rating>
<HelpfulVotes>0</HelpfulVotes>
<CustomerId>A3AMUC3ZLUP6HU</CustomerId>
<TotalVotes>0</TotalVotes>
<Date>2006-06-03</Date>
<Summary>Great Book.</Summary>
<Content>If you want to know the future of MMO games, then this is a must read.</Content>
</Review>
</CustomerReviews>
</Item>
</Items>
</ItemSearchResponse>


Prev | Next | Users Guide | PhantomScript Reference | P1 Systems

PhantomScript v1.7 Reference - Copyright © 2006 P1 Systems Incorporated. All Rights Reserved.