<?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>keyboardsamurais.de &#187; contains</title>
	<atom:link href="http://keyboardsamurais.de/tag/contains/feed/" rel="self" type="application/rss+xml" />
	<link>http://keyboardsamurais.de</link>
	<description>thoughts on software development, warts and all...</description>
	<lastBuildDate>Sun, 19 Jun 2011 12:36:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Matching XML content with XPath &#8211; the magic is in the &#8216;.&#8217;</title>
		<link>http://keyboardsamurais.de/2008/01/16/matching-xml-content-with-xpath-the-magic-is-in-the/</link>
		<comments>http://keyboardsamurais.de/2008/01/16/matching-xml-content-with-xpath-the-magic-is-in-the/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 15:03:54 +0000</pubDate>
		<dc:creator>Toni</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[contains]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[current]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[matching]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://keyboardsamurais.de/2008/01/16/matching-xml-content-with-xpath-the-magic-is-in-the/</guid>
		<description><![CDATA[Sometimes it's the simple things that make you wonder how they are done on a high level. For example, if you are wondering how you'd match the content of a node in Xpath, you might think to yourself: "Gee I really would like to know which food-nodes in my xml document contain the word 'Donuts' [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it's the simple things that make you wonder how they are done on a high level. For example, if you are wondering how you'd match the content of a node in Xpath, you might think to yourself: "Gee I really would like to know which food-nodes in my xml document contain the word 'Donuts' - this should be easy to find out with XPath".</p>
<p>Well, let me save you some time here. At first you might be looking for a function that matches strings. By looking into the <a href="http://www.w3.org/TR/xpath-functions/">w3c xpath function reference</a> you'd find a function called <code><a href="http://www.w3.org/TR/xpath-functions/#func-contains">contains()</a></code>. '<code>contains()</code>' takes two parameters. And here the fun starts. </p>
<p>What the reference doesn't tell you (ok I'm sure it does tell you <em>somewhere</em>), is how to fiddle your matches into the first contains() parameter, which incidentally is the text you want to scan. If your document looks something like this:</p>
<pre>
&lt;mydocument&gt;
   &lt;food&gt;Bavarian Donuts&lt;/food&gt;
   &lt;food&gt;Cheesecake&lt;/food&gt;
   &lt;food&gt;Oranges&lt;/food&gt;
&lt;/mydocument&gt;
</pre>
<p>Your first approach might look like this: <code>/mydocument/food[contains(/mydocument/food,'Donuts')]</code> Which, in a universe where everything makes perfect sense, would yield you one hit. However, this one just breaks your XPath processor, because contains() doesn't accept multiple matches as the first parameter.</p>
<p>Instead you can do this: <code>/mydocument/food[contains(.,'Donuts')]</code></p>
<p>The trick is that you use the <code>'.'</code> which is a shortcut for the current node function to reference the currently matched node as input to your <code>contains()</code> function. That's all there's to it.</p>
 <p><a href="http://keyboardsamurais.de/?flattrss_redirect&amp;id=222&amp;md5=e315c655dd46c8f2eca2e3846335555a" title="Flattr" target="_blank"><img src="http://keyboardsamurais.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://keyboardsamurais.de/2008/01/16/matching-xml-content-with-xpath-the-magic-is-in-the/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

