|
|
Sherlock 3 Developer Channels
Apple provides several tools to aid you in debugging your trigger
code. Developers can add the Channel
Development Tools subscription to Sherlock to access four special
channels: an XQuery code tester, a
JavaScript code tester, the XPath
Finder, and an HTML View tool.
|

|
The JavaScript
Channel
The JavaScript channel executes any code in the Java Script Souce
Source field and displays the results. Here are some samples you
can copy/paste and run: Sample
1
/* Get the Nth item in an array */
var itemList = new Array("a", "b", "c",
"d", "e", "f", "g");
/* JavaScript sequences are zero (not one) based */
var thirdItem = itemList[2];
return thirdItem;
Sample
2
/* Find the value for a given key in an object */
var dict = new Object();
dict.key1 = "cheetah";
dict.key2 = "puma";
dict.key3 = "jaguar";
var value = dict.key3;
return value
Sample
3
/* Open a URL in a browser */
/* This is done with a custom System object. */
System.OpenURL("http://www.apple.com");
|

|
The XQuery Channel
The XQuery channel executes any code in the XQuery souce text field
and displays the results. The text fields on the right hand side
of the channel window contain some convenient snippets that can
be copied and pasted into a channel.
Here are some samples you can copy/paste into the XQuery Source
field and run: Sample
1
{-- Get the HTML at the given URL --}
document("http://www.apple.com")
Sample 2
{-- Get the Nth item in a sequence --}
let $sequence := ("a", "b", "c", "d",
"e", "f", "g")
{-- XQuery sequences are one (not zero) based --}
let $thirdItem := $sequence[3]
{-- Note that we need a "return" once we have a "let"
statement --}
return $thirdItem
Sample 3
{-- Find the value for a given key in a dictionary.
Note that the dictionary object is an extension
to XQuery.
--}
let $dictionary := dictionary(("key1", "cheetah"),
("key2", "puma"),
("key3", "jaguar"))
let $value := dictionary-get($dictionary, "key3")
return $value
Sample 4
{-- Dealing with a conditional --}
let $value1 := if (true()) then "yes" else "no"
let $value2 := if (1) then "yes" else "no"
let $value3 := if (1=2-1) then "yes" else "no"
return ($value1, $value2, $value3)
Sample 5
{-- String manipulation --}
let $s1 := "Now is the time for all good men to come to the
aid of their country."
let $s2 := replace($s1, "all good men", "every man")
let $s3 := replace($s2, "their", "his")
return $s3
Sample 6
{-- Parsing an XML object --}
{-- Construct an XML object --}
let $src :=
<document>
<item type="a"><value>{"one"}</value
></item>
<item type="b"><value>{"two"}</value
></item>
</document>
{-- Get, as upper-case, the text of the "value" elements
of all "item" elements with a "type" attribute
of "b"
--}
return $src//item[@type="b"]/value/upper-case(.)
Sample 7
{-- Get the URL of every anchor on a given page --}
{-- Note that http-request is an extension to XQuery
which returns not just the HTTP data, but additional
data related to the request, such as the actual URL
the data is from (in the case of a re-direct).
--}
let $httpRequest := http-request("http://www.apple.com")
let $baseURL := $httpRequest/ACTUAL_URL/url()
let $html := $httpRequest/DATA
{-- XQuery is case sensitive; we need to get all uppercase
and lowercase anchors, and concatenate them using the
parenthesis operator.
--}
let $allAnchors := ($html//a, $html//A)
{-- Get all HREF and href URLs (again, case sensitive) --}
let $allHREFs := ($allAnchors/@href, $allAnchors/@HREF)
{-- Since the anchor URLs may be relative, we want to create URLs
that are relative to the actual base URL returned. Note that
full URLs will ignore the base URL, so we will get what we
want.
--}
let $allURLs := for $href in $allHREFs
{-- Note that url-with-base is an XQuery extension --}
return url-with-base($href, $baseURL)
return $allURLs |

|
The XPath Finder
Channel
The XPath Finder channel can be used to obtain the path to a particular
piece of data embedded in a web page. The XPath Finder channel parses
the HTML and builds a hierarchical tree of the HTML tags. As tags
are navigated using the NSBrowser (column view), this channel displays
the path to the currently selected item in the XQuery Path field
at the bottom of the page. For a given tag, use the corresponding
path in your XQuery code to retrieve the tag and its contents.
For example, to obtain the current Greenwich Mean Time, available
at a given URL, enter the URL in the URL field, and click Get HTML.
This will download and parse the HTML, populating the first column
of the NSBrowser (column view). As items are clicked in the column
view, two things happen: the parsed subtree is rendered in the HTMLView,
and the XQuery statement used to retrieve the subtree is computed
and displayed. Once this XQuery statement is obtained, it can be
used in a channel. |

|
The HTMLView
Channel
The HTMLView channel lets allows for experimenting with the HTMLView
control used by Sherlock channels. Clicking the Get HTML button
retrieves the HTML source from the URL in the topmost text field.
Clicking the Render button renders the HTML in the HTMLView in the
lower part of the channel window. HTML can be entered or edited
manually in the HTML text field and clicking the Render button will
show how the HTML will be rendered. |
|
|