<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Artful Code &#187; xml</title>
	<atom:link href="http://www.artfulcode.net/tags/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using newLISP&#8217;s find-all</title>
		<link>http://www.artfulcode.net/articles/using-newlisps-find-all/</link>
		<comments>http://www.artfulcode.net/articles/using-newlisps-find-all/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 15:29:47 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/using-newlisps-find-all/</guid>
		<description><![CDATA[newLISP&#8217;s find-all utility is exceptionally powerful, especially when coupled with rich matching functions like match, regex, and unify. find-all combines search and substitution into a fast, comprehensive function for extracting data from lists and strings. Basic syntax Like many newLISP sequence functions, find-all is defined for both strings and lists. The basic syntax is: Strings: [...]]]></description>
			<content:encoded><![CDATA[<p>newLISP&#8217;s <code>find-all</code> utility is exceptionally powerful, especially when coupled with rich matching functions like <code>match</code>, <code>regex</code>, and <code>unify</code>. <code>find-all</code> combines search and substitution into a fast, comprehensive function for extracting data from lists and strings.<span id="more-15"></span></p>
<h4>Basic syntax</h4>
<p>Like many newLISP sequence functions, <code>find-all</code> is defined for both strings and lists.  The basic syntax is:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;">Strings: <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> pattern target <span style="color: #AF0500;">&#91;</span>expression <span style="color: #AF0500;">&#91;</span>option<span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#41;</span>
Lists:   <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> pattern target <span style="color: #AF0500;">&#91;</span>expression <span style="color: #AF0500;">&#91;</span>comparison-function<span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h4>String searches and regular expressions</h4>
<p>The most basic string search finds occurrences of one string inside of another.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'str <span style="color: #3AA43E;">&quot;Now is the time for all good men to come to the aid of their country.&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;">&quot;the&quot;</span> str<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (&quot;the&quot; &quot;the&quot; &quot;the&quot;)</span></pre></div></div>

<p>There are three occurrences (including one within the word, &#8216;their&#8217;).  To find only occurrences of &#8216;the&#8217; as a complete word, a regular expression is used:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\bthe\b<span style="color: #AF0500;">&#125;</span></span> str <span style="color: #2028B8;">$0</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (&quot;the&quot; &quot;the&quot;)</span></pre></div></div>

<p>The first parameter is a regular expression, defined in curly braces to eliminate the need to double-escape entities (like b).  The final parameter, <code>0</code>, is the PCRE option (a full list of options is available in the newLISP documents for <a href="http://www.newlisp.org/downloads/newlisp_manual.html#regex">regex</a>.</p>
<p>The third parameter is key to one of the more powerful features of <code>find-all</code> &#8211; substitution.  The <code>expression</code> parameter is applied to each element found.  Inside of this expression, the global variable <code>$0</code> represents the entire matched element.  In a regular expression search, captured matches are available via subsequently enumerated variables: <code>$1</code>, <code>$2</code>, etc.</p>
<p>For example, to convert all occurrences of &#8216;the&#8217; to upper case in the resulting list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\bthe\b<span style="color: #AF0500;">&#125;</span></span> str <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">upper-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;(&quot;THE&quot; &quot;THE&quot;)</span></pre></div></div>

<p>Here is a short program to count the number of occurrences of some common words in the text of War and Peace:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'text <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">read-file</span> <span style="color: #3AA43E;">&quot;war_and_peace.txt&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b\S+\b<span style="color: #AF0500;">&#125;</span></span> text <span style="color: #2028B8;">$0</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; split into words</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%12s: %6d&quot;</span> <span style="color: #3AA43E;">&quot;Total words&quot;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">length</span> words<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">letn</span> <span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
       <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words words<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">dolist</span> <span style="color: #AF0500;">&#40;</span>word counts<span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%12s: %6d&quot;</span> <span style="color: #AF0500;">&#40;</span>word <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>word <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>With the results:</p>
<pre><code> Total words: 564345
         and:  21023
          or:   1542
         the:  31702
</code></pre>
<p>This isn&#8217;t necessarily efficient; finding all words and then counting occurrences of three specific words in a list of more than half a million elements results in a lot of wasted time.  The regular expression used also includes opening and closing punctuation in the word (i.e. a quote that begins &#8220;The&#8230;&#8221; would result in a missed occurrence of &#8216;the&#8217;, because the list would contain <em>&#8220;The</em> instead.  It also does not account for case when counting occurrences.</p>
<p>Here is a more efficient version:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'occurrences <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words occurrences<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((&quot;and&quot; 22267) (&quot;or&quot; 1582) (&quot;the&quot; 34619))</span></pre></div></div>

<p>Note that <code>find-all</code> is recursive and newLISP has a limited stack size, which can be set during execution with the -s switch:</p>
<pre><code>newlisp -s 500000 common_counts.lsp
</code></pre>
<p>The previous example would hit the default stack limit of 2,048.  This can be solved by using map instead of <code>find-all</code>&#8216;s substitution expression:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'occurrences <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">lower-case</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #2028B8;">$0</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words occurrences<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((&quot;and&quot; 22267) (&quot;or&quot; 1582) (&quot;the&quot; 34619))</span></pre></div></div>

<p>The substitution expression is also useful to cause side effects, squeezing even more efficiency out of the algorithm.  Here, <code>find-all</code> increments a count of each word and stores it in a dictionary.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> counts:<span style="color: #2028B8;">counts</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">or</span> <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">dolist</span> <span style="color: #AF0500;">&#40;</span>w common-words<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%4s: %6d&quot;</span> w <span style="color: #AF0500;">&#40;</span>counts w<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">; and:  22267</span>
 <span style="color: #808080; font-style: italic;">;  or:   1582</span>
 <span style="color: #808080; font-style: italic;">; the:  34619</span></pre></div></div>

<p>This algorithm is quite fast, although still somewhat weighty in RAM, since it stores the entire text before doing its work.</p>
<h4>List searches</h4>
<p><code>find-all</code> list searches are (arguably) nearly as powerful as regular expressions.  By default, <code>find-all</code> compares elements of target with pattern using <code>match</code>, and the default expression is the entire matched element (<code>$0</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'target '<span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>a <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>b <span style="color: #675400;">2</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>c <span style="color: #675400;">3</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>d <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">?</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span> target<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((a 1) (b 2) (c 3))</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span>d <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> target<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((d 4 5))</span></pre></div></div>

<p>Using the substitution expression, lists may be unified or matched further:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">?</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span> target <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unify</span> '<span style="color: #AF0500;">&#40;</span>Letter Number<span style="color: #AF0500;">&#41;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (((Letter a) (Number 1)) ((Letter b) (Number 2)) ((Letter c) (Number 3)))</span></pre></div></div>

<p>Simple list searches using other comparators than <code>match</code> are straight-forward:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #675400;">4</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">3</span> <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span> <span style="color: #2028B8;">$0</span> &amp;gt<span style="color: #808080; font-style: italic;">;)</span>
<span style="color: #808080; font-style: italic;">; (1 2 3)</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #675400;">4</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">3</span> <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> &amp;gt<span style="color: #808080; font-style: italic;">;)</span>
<span style="color: #808080; font-style: italic;">; (2 3 4)</span></pre></div></div>

<p><code>find-all</code> is convenient for searching XML.  newLISP parses XML into a tree.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">xml-type-tags</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'xml <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">xml-parse</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-url</span> <span style="color: #3AA43E;">&quot;http://www.weather.gov/xml/current_obs/index.xml&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">4</span> <span style="color: #675400;">16</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>For more info on parsing XML in newLISP, see <a href="http://www.artfulcode.net/articles/working-xml-newlisp/">this article</a> and the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#XML">newLISP documentation</a>.</p>
<p>The XML at the url above lists weather stations.  Here is an example station entry, under the root element, &#8220;wx_station_index&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>TAPA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>AG<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Vc Bird Intl Airport Antigua<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>17.117<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-61.783<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.noaa.gov/weather/current/TAPA.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/TAPA.rss<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/TAPA.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><code>assoc</code> is useful for finding a path in a parsed XML list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'stations-xml <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>However, <code>assoc</code> only returns the first element of the list that matches.  In the XML article linked above, <code>pop-assoc</code> was used to collect elements iteratively:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'stations '<span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">while</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span> <span style="color: #3AA43E;">&quot;station&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>That is a handy way of collecting all elements, especially in an irregular document.  With <code>find-all</code>, elements may be found just as easily and without modifying the original list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>From there, it is just as simple to aggregate the values from each element:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'station-pattern
    '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station_id&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;state&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station_name&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;latitude&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;longitude&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;html_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;rss_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;xml_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">match</span> station-pattern <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>This returns a list of the node values for each station.  The data can be applied to a different associative structure easily using <code>unify</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unify</span> '<span style="color: #AF0500;">&#40;</span>Id State <span style="color: #2028B8;">Name</span> Lat Lon Html Rss Xml<span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">match</span> station-pattern <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Each element in this list this creates looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>Id <span style="color: #3AA43E;">&quot;TAPA&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>State <span style="color: #3AA43E;">&quot;AG&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">Name</span> <span style="color: #3AA43E;">&quot;Vc Bird Intl Airport Antigua&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Lat <span style="color: #3AA43E;">&quot;17.117&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Lon <span style="color: #3AA43E;">&quot;-61.783&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Html <span style="color: #3AA43E;">&quot;http://weather.noaa.gov/weather/current/TAPA.html&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Rss <span style="color: #3AA43E;">&quot;http://weather.gov/xml/current_obs/TAPA.rss&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Xml <span style="color: #3AA43E;">&quot;http://weather.gov/xml/current_obs/TAPA.xml&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h4>A completely impractical example</h4>
<p>There are further implications of <code>find-all</code>&#8216;s ability to cause side effects in the substitution expression.  To download the XML for each individual station, something like this could be done (<em>don&#8217;t really do this &#8211; it will attempt to spawn more than 2,000 processes</em>):</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'processes
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
      <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">spawn</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sym</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lookup</span> <span style="color: #3AA43E;">&quot;station_name&quot;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> 'WEATHER<span style="color: #AF0500;">&#41;</span>
             <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-url</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lookup</span> <span style="color: #3AA43E;">&quot;xml_url&quot;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sync</span> <span style="color: #675400;">30000</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; wait 30 seconds for all downloads to finish</span></pre></div></div>

<p>Assuming that the application is able to download the XML file for more than 2,000 stations in 30 seconds (and that there is no hard limit to forked processes, as there is in OSX), the context <code>WEATHER</code> will contain the XML for all stations.</p>
<p>The infeasability of this example aside, it demonstrates, as an example, how easily a user-supplied XML file could be used to script a newLISP application.</p>
<h4>Further documentation</h4>
<ul>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#find-all">find-all</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#unify">unify</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#match">match</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#regex">regex</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#xml-parse">xml-parse</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Using+newLISP%26%238217%3Bs+find-all&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Using+newLISP%26%238217%3Bs+find-all+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/using-newlisps-find-all/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working with XML in newLISP</title>
		<link>http://www.artfulcode.net/articles/working-xml-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/working-xml-newlisp/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 15:56:10 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/working-xml-newlisp/</guid>
		<description><![CDATA[Version 9.3 of newLISP has been released and has new functions to facilitate working with XML. newLISP has had the ability to parse XML data into an association list since version 6.10. 9.3 adds the ability to more easily traverse a parsed document and edit its contents. What makes newLISP&#8217;s XML handling great? The majority [...]]]></description>
			<content:encoded><![CDATA[<p>Version 9.3 of newLISP has been released and has new functions to facilitate working with XML.  newLISP has had the ability to parse XML data into an association list since version 6.10.  9.3 adds the ability to more easily traverse a parsed document and edit its contents.<span id="more-37"></span></p>
<h4>What makes newLISP&#8217;s XML handling great?</h4>
<p>The majority of modern languages take an object oriented approach to XML.  They parse the document and store it in an object.  The ugliest use the functions defined in the DOM specification.  A couple have more expressive interfaces, such as PHP&#8217;s simplexml, or merely novel, such as Python&#8217;s event-based handling.</p>
<p>Each of these languages have list structures and an excellent, established array of functions for dealing with them.  So why use a complex, arcane interface?  Why not simply translate the XML into a list?</p>
<p>The fundamental structure in lisp is the linked list.  Lists can be of arbitrary length and depth and can be used to create infinitely complex structures.  newLISP parses an XML string and converts it into a list.</p>
<p>Lisp&#8217;s strength is list processing.  Working with XML as a list is therefore a simple task.  Elements can be matched and located using <code>match</code> and <code>unify</code> or by use of a recursive function.</p>
<h4>What doesn&#8217;t work?</h4>
<p>newLISP is lacking in a function to convert a list back to XML.  However, this is not too difficult to achieve on our own.</p>
<h4>A concrete example</h4>
<p>I recently built an application to locally cache information from another program which stores photos and publishes photos&#8217; metadata via XML.  The application that uses the cached data is written in Python, but I have never liked the way that Python (or most languages) deal with XML, so I decided to write this module in newLISP.</p>
<p>To download the remote document:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>get-xml-data url<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>xml-data <span style="color: #66cc66;">&#40;</span>get-url url <span style="color: #cc66cc;">3000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>regex <span style="color: #ff0000;">&quot;ERR: (.+?)$&quot;</span> xml-data<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>throw-<span style="color: #b1b100;">error</span> <span style="color: #66cc66;">&#40;</span>format <span style="color: #ff0000;">&quot;download error: %s&quot;</span> $<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        xml-data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This function downloads a url and returns it as a string.  If the host times out after 3000 milliseconds or there is another error (which <code>get-url</code> stores in its return value preceded by &#8220;ERR:&#8221;), we throw an error.  We match this using <code>regex</code> so that any error text can be captured in the system variable <code>$1</code>.  Otherwise the XML is returned as a string.</p>
<p>We can then define a function to parse the result and verify its validity:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>parse-xml-data str<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>xml-type-tags <span style="color: #b1b100;">nil</span> <span style="color: #ff0000;">&quot;CDATA&quot;</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>xml <span style="color: #66cc66;">&#40;</span>xml-parse str <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> xml <span style="color: #66cc66;">&#40;</span>throw-<span style="color: #b1b100;">error</span> <span style="color: #66cc66;">&#40;</span>xml-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Let&#8217;s walk through this function.  First, <code>xml-type-tags</code> defines how we want <code>xml-parse</code> to deal with TEXT, CDATA, COMMENT, and ELEMENT type tags.  By default, these are included as strings in each node.  I only want to know if something is CDATA and may contain oddball characters.</p>
<p>The <code>xml-parse</code> function&#8217;s third parameter is formed by adding the value of various options together.  Granted this is a very C-like approach and not extremely expressive, but hey, life isn&#8217;t fair.  Here is a list of the options and values (from the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#xml-parse">newLISP documentation</a>):</p>
<pre><code>1    suppress whitespace text tags
2    suppress empty attribute lists
4    suppress comment tags
8    translate string tags into symbols
16   add SXML (S-expression XML) attribute tags
</code></pre>
<p>From this we can see that we have remove whitespace tags and comments.  I do not recommend suppressing empty attribute lists; there is little way to intuit then whether or not the first item in an element is its attribute list or a child element.  We then evaluate to either the XML data or, if XML is nil (meaning there was an error parsing the XML), we throw an error.</p>
<p>We now have our data.  Here is a fragment of the XML document and its resulting parsed list:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
        9374
        /some/path/on/the/server
        something.jpg
something_thumb.jpg
        January 9, 2008 4:55 PM EST
        January 9, 2008 5:20 PM EST
&nbsp;
        9375
        /some/path/on/the/server
        something2.jpg
something_thumb2.jpg
        January 9, 2008 4:56 PM EST
        January 9, 2008 5:21 PM EST</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">'<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;album&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media&quot;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;type&quot;</span> <span style="color: #ff0000;">&quot;photo&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;9374&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media_root&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;/some/path/on/the/server&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fullsize&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;something.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;something_thumb.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;authored&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;January 9, 2008 4:55 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;submitted&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;January 9, 2008 5:20 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media&quot;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;type&quot;</span> <span style="color: #ff0000;">&quot;photo&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;9375&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media_root&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;/some/path/on/the/server&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fullsize&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;something2.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;something_thumb2.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;authored&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;January 9, 2008 4:56 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;submitted&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;January 9, 2008 5:21 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The similarities between XML and lisp lists are obvious (as is the wordiness of XML.)</p>
<p>Accessing the first media element is simple: <code>(assoc (xml "album" "media"))</code>.  Note that the full path is used to find the nested association.  However, that will return only the first association found for the key &#8220;media&#8221;.  What if we want all media elements?</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>media xml <span style="color: #66cc66;">,</span> lst<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>while <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> <span style="color: #66cc66;">&#40;</span>xml <span style="color: #ff0000;">&quot;album&quot;</span> <span style="color: #ff0000;">&quot;media&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>push <span style="color: #66cc66;">&#40;</span>pop-<span style="color: #b1b100;">assoc</span> <span style="color: #66cc66;">&#40;</span>xml <span style="color: #ff0000;">&quot;album&quot;</span> <span style="color: #ff0000;">&quot;media&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> lst -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
lst<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This function shows how <code>pop-assoc</code> can be used to reduce a list.<br />
<code>pop-assoc</code> returns the popped association, which is then pushed onto the list that gets returned.  The result of this function:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">'<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media&quot;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #ff0000;">&quot;9374&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media_root&quot;</span> <span style="color: #ff0000;">&quot;/some/path/on/the/server&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fullsize&quot;</span> <span style="color: #ff0000;">&quot;something.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #ff0000;">&quot;something_thumb.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;authored&quot;</span> <span style="color: #ff0000;">&quot;January 9, 2008 4:55 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;submitted&quot;</span> <span style="color: #ff0000;">&quot;January 9, 2008 5:20 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media&quot;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #ff0000;">&quot;9375&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;media_root&quot;</span> <span style="color: #ff0000;">&quot;/some/path/on/the/server&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fullsize&quot;</span> <span style="color: #ff0000;">&quot;something2.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #ff0000;">&quot;something_thumb2.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;authored&quot;</span> <span style="color: #ff0000;">&quot;January 9, 2008 4:56 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;submitted&quot;</span> <span style="color: #ff0000;">&quot;January 9, 2008 5:21 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>Exporting a list to XML</h4>
<p>This is an area where newLISP is lacking.  There is no function to return a list to XML.  To that end I&#8217;ve written a basic XML module to automate parsing XML in a defined manner and then writing the list back to XML.  One neat thing I have done in this module is to make the <code>xml-type-tag</code> symbols evaluate to macros that each know how to render a node as a string.</p>
<p>So long as the sxml conventions are maintained (a full element is <code>(element "tag-name" (@ attribute-list) child-node-list)</code>, i.e. <code>(XML:element "a" (@ ("href" "http://www.artfulcode.net")) ((XML:text "An excellent website")))</code> =&gt; <code>&lt;a href="http://www.artfulcode.net"&gt;An excellent website&lt;/a&gt;</code>) and empty attributes lists are provided (as <code>(@)</code>), conversion back and forth is straigh-forward.</p>
<p>If the XML string that is parsed has a declaration, it will be maintained.  Otherwise, it uses a generic UTF-8 declaration.  Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>context 'XML<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'declaration <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&amp;</span>lt<span style="color: #808080; font-style: italic;">; ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&amp;gt;})</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Formats plain text.&quot;</span>
  <span style="color: #66cc66;">&#40;</span>string <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>comment<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Formats comments.&quot;</span>
  <span style="color: #66cc66;">&#40;</span>format <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&lt;!</span>-- <span style="color: #66cc66;">%</span>s --<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#40;</span>string <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>cdata<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Formats CDATA nodes.&quot;</span>
  <span style="color: #66cc66;">&#40;</span>format <span style="color: #ff0000;">&quot;&amp;lt; ![CDATA[%s]]&amp;gt;&quot;</span> <span style="color: #66cc66;">&#40;</span>string <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>@ attr<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Turns an s-xml attribute list into an XML attribute list.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span>? attr<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>? attr<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>format <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">%</span>s<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #66cc66;">%</span>s<span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#40;</span>string <span style="color: #66cc66;">&#40;</span>attr <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>string <span style="color: #66cc66;">&#40;</span>attr <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> @ <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>element tag-<span style="color: #b1b100;">name</span> attributes children<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Recursively evaluates the contents of the s-xml list.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>string-format <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&amp;</span>lt<span style="color: #808080; font-style: italic;">; %s%s&amp;lt;%s&amp;gt;/%s&amp;gt;})</span>
        <span style="color: #66cc66;">&#40;</span>items <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>string tag-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span>
                     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> attributes<span style="color: #66cc66;">&#41;</span>
                     <span style="color: #66cc66;">&#40;</span>join <span style="color: #66cc66;">&#40;</span>map <span style="color: #b1b100;">eval</span> children<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
                     <span style="color: #66cc66;">&#40;</span>string tag-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>format string-format items<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>xml-<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">;list xml-string)</span>
  <span style="color: #ff0000;">&quot;Parses XML string xml-string. Returns a list in s-xml format.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>starts-with xml-string <span style="color: #ff0000;">&quot;&amp;lt; ?xml&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>begin
      <span style="color: #66cc66;">&#40;</span>regex <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">^</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>lt<span style="color: #808080; font-style: italic;">;\?xml .+? \?&amp;gt;)$} xml-string 16)</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'declaration $<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>xml-type-tags 'text 'cdata 'comment 'element<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>xml-parse xml-string <span style="color: #cc66cc;">17</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>list-<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">;xml xml-lst)</span>
  <span style="color: #ff0000;">&quot;Evaluates an s-xml-formatted list to XML.&quot;</span>
  <span style="color: #66cc66;">&#40;</span>format <span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>%s&quot;</span> declaration <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> xml-lst<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>context MAIN<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The declaration can be manually set via the symbol <code>XML:declaration</code>.  The XML node symbols <code>element</code>, <code>text</code>, <code>comment</code>, <code>cdata</code>, <code>text</code>, and <code>@</code> are all expected to be XML context symbols.  The main forward-facing functions are <code>xml-&gt;list</code> and <code>list-&gt;xml</code>.  They are used, simply enough, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'xml-string <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #cc66cc;">9374</span>
        /some/path/on/the/server
        something<span style="color: #66cc66;">.</span>jpg
something_thumb<span style="color: #66cc66;">.</span>jpg
        January <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2008</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">:</span><span style="color: #cc66cc;">55</span> PM EST
        January <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2008</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">:</span><span style="color: #cc66cc;">20</span> PM EST
&nbsp;
        <span style="color: #cc66cc;">9375</span>
        /some/path/on/the/server
        something2<span style="color: #66cc66;">.</span>jpg
something_thumb2<span style="color: #66cc66;">.</span>jpg
        January <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2008</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">:</span><span style="color: #cc66cc;">56</span> PM EST
        January <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2008</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">:</span><span style="color: #cc66cc;">21</span> PM EST
&nbsp;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'xml-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">xml-</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">;list xml-string))</span></pre></div></div>

<p>Now, <code>xml-list</code> is:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;album&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;media&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;9374&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;media_root&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;/some/path/on/the/server&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;fullsize&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;something.jpg&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;something_thumb.jpg&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;authored&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;January 9, 2008 4:55 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;submitted&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;January 9, 2008 5:20 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;media&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;9375&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;media_root&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;/some/path/on/the/server&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;fullsize&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;something2.jpg&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;something_thumb2.jpg&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;authored&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;January 9, 2008 4:56 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">element</span> <span style="color: #ff0000;">&quot;submitted&quot;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">text</span> <span style="color: #ff0000;">&quot;January 9, 2008 5:21 PM EST&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We can convert back with <code>XML:list-&gt;xml</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'new-xml-string <span style="color: #66cc66;">&#40;</span>XML<span style="color: #66cc66;">:</span><span style="color: #555;">list-</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">;xml xml-list))</span></pre></div></div>

<p>Which makes <code>new-xml-string</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #ddbb00;">&amp;lt;</span> ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?<span style="color: #ddbb00;">&amp;gt;</span>
&nbsp;
        9374
        /some/path/on/the/server
        something.jpg
something_thumb.jpg
        January 9, 2008 4:55 PM EST
        January 9, 2008 5:20 PM EST
&nbsp;
        9375
        /some/path/on/the/server
        something2.jpg
something_thumb2.jpg
        January 9, 2008 4:56 PM EST
        January 9, 2008 5:21 PM EST</pre></div></div>

<p>The only difference is that the new string has an XML declaration.</p>
<p><a href="/wp-content/uploads/2008/12/xmllsp.gz">download</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F&amp;title=Working+with+XML+in+newLISP" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F&amp;title=Working+with+XML+in+newLISP" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Working+with+XML+in+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F&amp;title=Working+with+XML+in+newLISP" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F&amp;title=Working+with+XML+in+newLISP" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F&amp;title=Working+with+XML+in+newLISP" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Working+with+XML+in+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fworking-xml-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/working-xml-newlisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

