<?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>yesJames.com &#187; Ajax</title>
	<atom:link href="http://www.yesjames.com/index.php/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yesjames.com</link>
	<description>ahuh... sure... what ever you say...</description>
	<lastBuildDate>Fri, 13 Nov 2009 02:59:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Firefox compatible xPath functions in JavaScript</title>
		<link>http://www.yesjames.com/index.php/2008/11/firefox-compatible-xpath-functions-in-javascript/</link>
		<comments>http://www.yesjames.com/index.php/2008/11/firefox-compatible-xpath-functions-in-javascript/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 01:41:27 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XPath]]></category>

		<guid isPermaLink="false">http://www.yesjames.com/?p=74</guid>
		<description><![CDATA[There are a lot of quirks between different browsers, in particular though are the differences in the way each browser handles the DOM in JavaScript.
Internet Explorer, for the most part, implements MSXml 4.0 or higher. However that&#8217;s (obviously) a Microsoft technology, and the standard implementation doesn&#8217;t support XPath in the DOM.
In particular the selectNodes() and [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript" language="javascript" src="http://www.yesjames.com/wp-content/themes/yesjames_blue/sh/shBrushXml.js"></script>There are a lot of quirks between different browsers, in particular though are the differences in the way each browser handles the DOM in JavaScript.</p>
<p>Internet Explorer, for the most part, implements MSXml 4.0 or higher. However that&#8217;s (obviously) a Microsoft technology, and the standard implementation doesn&#8217;t support XPath in the DOM.</p>
<p>In particular the <strong>selectNodes()</strong> and <strong>selectSingleNode()</strong> functions are of particular use when manipulating XML or the DOM. Using these functions you can parse an XPath expression and get a nodelist of elements in your DOM that match the expression.</p>
<p><span id="more-74"></span></p>
<p>The code below adds basic XPath parsing functionality to the DOM in Mozilla. (Thanks to km0ti0n)</p>
<pre name="code" class="javascript">
// mozXPath - http://km0.la/js/mozXPath/
// km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License
// http://creativecommons.org/licenses/by-sa/2.5/
if (document.implementation.hasFeature("XPath", "3.0")) {
	if ( typeof XMLDocument == "undefined" ) {
		XMLDocument = Document;
	}
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
		if ( !xNode ) { xNode = this; }
		var oNSResolver = this.createNSResolver(this.documentElement);
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		var aResult = [];
		for ( var i = 0; i < aItems.snapshotLength; i++) {
			aResult[i] =  aItems.snapshotItem(i);
		}
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
		if( !xNode ) { xNode = this; }
		var xItems = this.selectNodes(cXPathString, xNode);
		if ( xItems.length > 0 ) { return xItems[0]; }
		else { return null; }
	}

	Element.prototype.selectNodes = function(cXPathString) {
		if (this.ownerDocument.selectNodes) {
			return this.ownerDocument.selectNodes(cXPathString, this);
		} else { throw "For XML Elements Only"; }
	}

	Element.prototype.selectSingleNode = function(cXPathString) {
		if (this.ownerDocument.selectSingleNode) {
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		} else { throw "For XML Elements Only"; }
	}
}
</pre>
<p>It appears that Opera doesn&#8217;t have the XMLDocument object type, so, when using the XmlHttpRequest object the <em>responseXML</em> is in fact a <em>Document</em> object.</p>
<p>To get around this fact, we can use:</p>
<pre name="code" class="javascript">
if ( typeof XMLDocument == "undefined" ) {
    XMLDocument = Document;
}
</pre>
<p>To use these new features, in either IE or Firefox, you can now simple call the functions as a method of the elements themselves&#8230; e.g.:</p>
<p>On the following XML Document:</p>
<pre name="code" class="xml">
<MainNode>
    <SubNode id="1" enabled="true">
      <ContentNode>Some Content</ContentNode>
    </SubNode>
    <SubNode id="2" enabled="true">
      <ContentNode>Some Content</ContentNode>
    </SubNode>
    <SubNode id="3" enabled="false">
      <ContentNode>Some Content</ContentNode>
    </SubNode>
</MainNode>
</pre>
<p>For the XML sample above, you can use the following XPath to select all <em>ContentNode</em>&#8217;s with their <em>enabled</em> attribute set to <em>&#8220;true&#8221;</em>:<br />
<code>//subnode[@enabled='true']/contentnode</code></p>
<p>So you would retrieve your XML Node List by executing the following JavaScript function:</p>
<pre name="code" class="javascript">
//assume the variable xDOM is an XmlDocument object
//...that has been pre-loaded with the example XML
var nodeList = xDOM.selectNodes('//subnode[@enabled='true']/contentnode');
for each (var xNode in nodeList) {
    //do some operation...
}
</pre>
<p>This would perform some operation on each <em>contentnode</em> which is inside a <em>subnode</em> with it&#8217;s <em>enabled</em> property set to <em>true</em>.</p>
<p><em><strong>NOTE:</strong> Be sure to double check your syntax, spelling and remember XML/XPath is CaseSensitive.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yesjames.com/index.php/2008/11/firefox-compatible-xpath-functions-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
