<?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>Erik A. Hanson &#187; XSL</title>
	<atom:link href="http://www.eahanson.com/category/xsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eahanson.com</link>
	<description>My weblog</description>
	<lastBuildDate>Thu, 06 Jan 2011 02:27:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Setting XSL Parameters from Javascript in AJAXSLT</title>
		<link>http://www.eahanson.com/2005/10/23/setting-xsl-parameters-from-javascript-in-ajaxslt/</link>
		<comments>http://www.eahanson.com/2005/10/23/setting-xsl-parameters-from-javascript-in-ajaxslt/#comments</comments>
		<pubDate>Sun, 23 Oct 2005 23:34:22 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=6</guid>
		<description><![CDATA[Recently, I was trying to pass some info from my Javascript into my XSL stylesheet. Here's how I finally did it.]]></description>
			<content:encoded><![CDATA[<p>Google&#8217;s <a href="http://goog-ajaxslt.sourceforge.net/">AJAXSLT</a> library is very nice, but it is lacking in documentation and sometimes hard to figure out.</p>
<p>
Recently, I was trying to pass some info from my Javascript into my XSL stylesheet. Here&#8217;s how I finally did it:</p>
<p>
<i>(Quick note: in real life, I don&#8217;t name variables &quot;myThis&quot; and &quot;myThat&quot;; I&#8217;m just doing it here to make it clear what is my code and what is part of XSL, AJAXSLT, Javascript, etc.)</i></p>
<p><h4>The XSL</h4>
<pre>
&lt;xsl:stylesheet version=&quot;1.0&quot;
    xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:param name=&quot;myParam&quot; select=&quot;&apos;myDefault&apos;&quot;/&gt;
  &lt;xsl:template match=&quot;/&quot;&gt;
    myParam: &lt;xsl:value-of select=&quot;$myParam&quot;/&gt;
  &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p><h4>The Javascript</h4>
<pre>
var myContext = new ExprContext(myXml);
myContext.setVariable(&quot;myParam&quot;,
    <b>new StringValue(&quot;the value for myParam&quot;)</b>);
xsltProcessContext(myContext, myXsl, myRootElement);
</pre>
<h4>The Trick</h4>
<p>So the big trick, which I figured out after reading lots of the AJAXSLT code, was to set the parameter to a <tt>StringValue</tt>, rather than a regular Javascript String. <tt>StringValue</tt> is part of AJAXSLT. <b>Update:</b> Will, in the comments below, mentions that there are four total types: <tt>StringValue</tt>, <tt>NumberValue</tt>, <tt>BooleanValue</tt> and <tt>NodeSetValue</tt>.</p>
<h4>Troubleshooting</h4>
<p>Don&#8217;t forget to declare your parameter with <code>&lt;xsl:param.../&gt;</code> before using it with <code>&lt;xsl:value-of select=&quot;$...&quot;/&gt;</code>, otherwise Ajaxslt will blow up in the middle of rendering.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2005/10/23/setting-xsl-parameters-from-javascript-in-ajaxslt/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How To Use Google&#8217;s Ajaxslt Library</title>
		<link>http://www.eahanson.com/2005/09/30/how-to-use-googles-ajaxslt-library/</link>
		<comments>http://www.eahanson.com/2005/09/30/how-to-use-googles-ajaxslt-library/#comments</comments>
		<pubDate>Fri, 30 Sep 2005 15:53:48 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=4</guid>
		<description><![CDATA[Google has written an open-source library called Ajaxslt that implements XPath and XSLT in Javascript. It took a bit of playing to get it to work.]]></description>
			<content:encoded><![CDATA[<p><a href="http://search.yahoo.com/search?p=google&#038;sm=Yahoo%21+Search&#038;fr=FP-tab-web-t&#038;toggle=1&#038;cop=&#038;ei=UTF-8">Google</a> has written an open-source library called <a href="http://goog-ajaxslt.sourceforge.net/">Ajaxslt</a> that implements <a href="http://www.w3.org/TR/1999/REC-xpath-19991116">XPath</a> and <a href="http://www.w3.org/TR/1999/REC-xslt-19991116">XSLT</a> in Javascript.</p>
<p>It took a bit of playing to get it to work. Here&#8217;s some sample code. (I forgot to write it up when I did it, so I might have left something out.)</p>
<h3>Setup</h3>
<p>First, include the scripts. <b>Note:</b> make sure to have a closing <tt>&lt;/script&gt;</tt> tag rather than doing <tt>&lt;script ... /&gt;</tt> because that silently fails on some browsers (yay!).</p>
<pre>
&lt;!-- AJAXSLT --&gt;
&lt;script src=&quot;/ajaxslt/misc.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/dom.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/xpath.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/xslt.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/xpathdebug.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
</pre>
<h3>Using Ajaxslt for XSL Transformation</h3>
<p>For XSL transformation, you&#8217;ll need three things: some XML, some XSL, and a node (or DocumentFragment presumably) to place the result in. One way to get the XML and XSL is to load them through an XmlHttpRequest. Ajaxslt also provides a couple of handy functions to convert between text and XML:</p>
<pre>
var myText = &quot;...&quot;;
var myXml = xmlParse(myText);
var myTextAgain = xmlText(myXml);
</pre>
<p>Once you&#8217;ve got the XML and XSL, you&#8217;re ready to do the transformation:</p>
<pre>
var myNode = document.getElementById(&quot;myId&quot;);
xsltProcessContext(new ExprContext(myXml), myXsl, myNode);
</pre>
<p>Your node should now contain the result of the transformation.</p>
<h3>Using Ajaxslt for XPath</h3>
<p>You can also use Ajaxslt for XPath evaluation, which is really handy when writing tests on your rendered document:</p>
<pre>
var expr = xpathParse(myXpathString);
var result = expr.evaluate(new ExprContext(myXmlDocument));
var resultAsString = result.stringValue();
</pre>
<h3>Troublshooting</h3>
<p><b>The order of the JavaScript include statements matters:</b> if Ajaxslt is giving strange errors, make sure the scripts are included in the exact same order as  in the Setup section of this page.</p>
<p><b>Make sure you know what you&#8217;re passing to Ajaxslt:</b> if you&#8217;re sending <tt>request.responseXML</tt> to a method, don&#8217;t look at <tt>request.responseText</tt> to see what you&#8217;re passing; use <tt>xmlText(request.responseXML)</tt>.</p>
<p><b>The root element isn&#8217;t selectable:</b> if your XML is <tt>&lt;foo id=&quot;myId&quot;&gt;...&lt;/foo&gt;</tt>, then  you don&#8217;t select foo&#8217;s id with <tt>/foo/@id</tt> but rather with just <tt>@id</tt>.</p>
<p><b>Case matters in xpath:</b> If you&#8217;re evaluating an xpath expression on an HTML DOM, note that the node names are most likely uppercase. So do <tt>/HTML/BODY</tt> instead of <tt>/html/body</tt></p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2005/09/30/how-to-use-googles-ajaxslt-library/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

