Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML variable -> JavaScript function 1

Status
Not open for further replies.

phobe62

Programmer
Jan 30, 2004
22
US
I have an XML variable that I want to pass into JavaScript. Something like:

<script language=&quot;JavaScript&quot;>
doSetM(VARIABLE);
</script>

The variable is called numBoats. How would I go about doing that? Some sample code would be much appreciated! Thanks.
 
Also, this produces an error:

......
<tr>
<td align=&quot;center&quot; valign=&quot;center&quot;>
<table style=&quot;border: 1px solid #000000&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<xsl:variable name=&quot;numBoats&quot; select=&quot;0&quot;/>
<xsl:for-each select=&quot;data/boat&quot;>
<xsl:variable name=&quot;numBoats&quot; select=&quot;$numBoats+1&quot;/>
<tr>
<td>
<table border=&quot;0&quot; cellpadding=&quot;4&quot; cellspacing=&quot;0&quot;>
<tr>
......

The error is that variables can't be used in namespace. How might I be able to fix that? Thanks.
 
If you use MSXML.DOMDocument, you can read any node using XPath expressions:
Code:
Doc.SelectSingleNode(&quot;xx/yy/zz&quot;).Text
The other way round: you can also pass variables to a stylesheet if you load in a MSXML.IXSLProcessor:
Code:
var xslt = new ActiveXObject(&quot;Msxml2.XSLTemplate.4.0&quot;);
var xslDoc = new ActiveXObject(&quot;Msxml2.FreeThreadedDOMDocument.4.0&quot;);
var xslProc;
xslDoc.async = false;
xslDoc.load(&quot;sample.xsl&quot;);
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject(&quot;Msxml2.DOMDocument.4.0&quot;);
xmlDoc.async = false;
xmlDoc.load(&quot;books.xml&quot;);
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.addParameter(&quot;param1&quot;, &quot;Hello&quot;);
xslProc.transform();
alert(xslProc.output);
If your error actually mentions namespace, I wonder if you posted the right snippet: this doesn't seem to be namespace-declaration.
I think though, that
Code:
<xsl:variable name=&quot;numBoats&quot; select=&quot;number($numBoats)+1&quot;/>
might help if $numBoats is a node (instead of a numeric value)
 
That's definitely the snippet. Exact error message:
Keyword xsl:variable may not be used in namespace

When I take out the line above and the line below the line that does the loop (<xml:for-each...), the error goes away.

On the other issue, let me say that I'm new to this. :) (Of course, that was probably pretty obvious already.) The one-line code is very appealing; how would I use it for a number variable ($numBoats).

Thanks for your help.
 
javascript:
<script language=&quot;JavaScript&quot;>
<xsl:text disable-output-escaping=&quot;yes&quot;><![CDATA[doSetM(]]></xsl:text>
<xsl:value-of select=&quot;$numBoats&quot;/>
<xsl:text disable-output-escaping=&quot;yes&quot;><![CDATA[);]]></xsl:text>
</script>

looping:
 
O, I overlooked: you cannot assign the same variable (numBoats)twice...
In most cases that's no problem, for example, you seem to use it to count something. You can use the count() or position() function for that.
 
Thanks guys. Now, for the loop... I haven't been able to get a useful answer from any web sites. Could I get a small example snippet that shows how to add 1 to the variable $numBoats each time it loops?
 
Actually, never mind on the loop. I just used JavaScript. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top