<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Comparing current node to previous node with XSLT and XPath</title>
	<link>http://ask.metafilter.com/90215/Comparing-current-node-to-previous-node-with-XSLT-and-XPath/</link>
	<description>Comments on Ask MetaFilter post Comparing current node to previous node with XSLT and XPath</description>
	<pubDate>Wed, 30 Apr 2008 14:10:02 -0800</pubDate>
	<lastBuildDate>Wed, 30 Apr 2008 14:10:02 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Comparing current node to previous node with XSLT and XPath</title>
		<link>http://ask.metafilter.com/90215/Comparing-current-node-to-previous-node-with-XSLT-and-XPath</link>	
		<description>I need an XPath expression that will allow me (in XSLT) to compare the value of an XML element in one node with the value of another element in a preceding node.  &lt;br /&gt;&lt;br /&gt; So, given this faked up code:&lt;br&gt;
&lt;br&gt;
&amp;lt;calendar&amp;gt;&lt;br&gt;
&amp;lt;event&amp;gt;&lt;br&gt;
&amp;lt;date&amp;gt;May 11&amp;lt;/date&amp;gt;&lt;br&gt;
&amp;lt;description&amp;gt;Mother&apos;s Day&amp;lt;/description&amp;gt;&lt;br&gt;
&amp;lt;/event&amp;gt;&lt;br&gt;
&amp;lt;event&amp;gt;&lt;br&gt;
&amp;lt;date&amp;gt;May 12&amp;lt;/date&amp;gt;&lt;br&gt;
&amp;lt;description&amp;gt;Birthday&amp;lt;/description&amp;gt;&lt;br&gt;
&amp;lt;/event&amp;gt;&lt;br&gt;
&amp;lt;event&amp;gt;&lt;br&gt;
&amp;lt;date&amp;gt;May 12&amp;lt;/date&amp;gt;&lt;br&gt;
&amp;lt;description&amp;gt;Board Meeting&amp;lt;/description&amp;gt;&lt;br&gt;
&amp;lt;/event&amp;gt;&lt;br&gt;
&amp;lt;calendar&amp;gt;&lt;br&gt;
&lt;br&gt;
I want to use XSLT to return something that looks like this:&lt;br&gt;
&lt;br&gt;
May 11&lt;br&gt;
----------&lt;br&gt;
Mother&apos;s Day&lt;br&gt;
&lt;br&gt;
May 12&lt;br&gt;
----------&lt;br&gt;
Birthday&lt;br&gt;
Board Meeting&lt;br&gt;
&lt;br&gt;
The problem is, I can&apos;t figure out how to compare the value of date in the current node to date in the previous node. &lt;br&gt;
&lt;br&gt;
I was trying&lt;br&gt;
&amp;lt;xsl:if test=&quot;date != preceding-sibling::event/date&quot;&amp;gt; but the problem is preceding-sibling is every sibling, and it defaults to the first sibling. I want the exact previous node -- if you&apos;re on X event node, compare the value of date to the value of date in X-1 event node. Since the total number of possible event nodes could be anywhere from 1 to infinite,  I can&apos;t just write code that goes event[3] vs event[2].&lt;br&gt;
&lt;br&gt;
I know there&apos;s a simple way to do this, but I&apos;ve just about exhausted my Google-fu. Ideas?</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2008:site.90215</guid>
		<pubDate>Wed, 30 Apr 2008 13:47:08 -0800</pubDate>
		<dc:creator>dw</dc:creator>
		
			<category>xml</category>
		
			<category>xsl</category>
		
			<category>xslt</category>
		
			<category>xpath</category>
		
	</item> <item>
		<title>By: thoughtless</title>
		<link>http://ask.metafilter.com/90215/Comparing-current-node-to-previous-node-with-XSLT-and-XPath#1324713</link>	
		<description>Forgive me if I&apos;m misunderstanding, but have you tried something equivalent to (forgive pseudo-code) -&lt;br&gt;
&lt;br&gt;
for-each //event&lt;br&gt;
&amp;nbsp;&amp;nbsp;if not(date/value() = ../event[position()-1]/date/value())&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print date&lt;br&gt;
&lt;br&gt;
? Assuming that&apos;s what you&apos;re looking to do. You could alternatively do something starting with for-each(distinct-values(//date/value())), I think, and come at it that way. That would have the advantage of not depending on the order of events.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.90215-1324713</guid>
		<pubDate>Wed, 30 Apr 2008 14:10:02 -0800</pubDate>
		<dc:creator>thoughtless</dc:creator>
	</item><item>
		<title>By: Khalad</title>
		<link>http://ask.metafilter.com/90215/Comparing-current-node-to-previous-node-with-XSLT-and-XPath#1324743</link>	
		<description>preceding-sibling ought to work, it&apos;s a reverse axis so the nodes are returned in reverse order. I think you just need to add a [1] so you&apos;re comparing only against one node instead of against all of the preceding siblings (which will likely succeed since the &apos;!=&apos; test will succeed ANY node on the left is not-equal to ANY node on the right). That is:&lt;br&gt;
&lt;br&gt;
date != preceding-sibling::event[1]/date&lt;br&gt;
&lt;br&gt;
That said, I am just spouting this off the top of my head so I can only hope I&apos;m right!</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.90215-1324743</guid>
		<pubDate>Wed, 30 Apr 2008 14:40:09 -0800</pubDate>
		<dc:creator>Khalad</dc:creator>
	</item><item>
		<title>By: gsteff</title>
		<link>http://ask.metafilter.com/90215/Comparing-current-node-to-previous-node-with-XSLT-and-XPath#1324778</link>	
		<description>&lt;a href=&quot;http://pygments.org/demo/745/&quot;&gt;This&lt;/a&gt; should do it!</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.90215-1324778</guid>
		<pubDate>Wed, 30 Apr 2008 15:03:12 -0800</pubDate>
		<dc:creator>gsteff</dc:creator>
	</item><item>
		<title>By: gsteff</title>
		<link>http://ask.metafilter.com/90215/Comparing-current-node-to-previous-node-with-XSLT-and-XPath#1324780</link>	
		<description>FYI, the preserve-space accomplishes nothing... xsl:text was easier.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.90215-1324780</guid>
		<pubDate>Wed, 30 Apr 2008 15:04:36 -0800</pubDate>
		<dc:creator>gsteff</dc:creator>
	</item><item>
		<title>By: dw</title>
		<link>http://ask.metafilter.com/90215/Comparing-current-node-to-previous-node-with-XSLT-and-XPath#1324880</link>	
		<description>Arrgh. It was the &quot;reverse axis&quot; part of preceding-sibling I was missing. Thank you!</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.90215-1324880</guid>
		<pubDate>Wed, 30 Apr 2008 16:55:27 -0800</pubDate>
		<dc:creator>dw</dc:creator>
	</item>
	</channel>
</rss>
