If you're still not sure what it does I'm just expanding on what Karl
wrote above ....
Given your code of...
for (var i = 0; i < 10; ++i)
{
var n = $($("virtual-sensor",data).get(i)).attr("name");
if (n!=null) gsn.vsbox.add(n); // add the selection to a box
}
...the line ...
var n = $($("virtual-sensor",data).get(i)).attr("name");
...breaks down to:
1. $("virtual-sensor", data)
$( selector , context) : Get all the elements of type 'virtual-
sensor' that exist in the 'data' context, returning them as a jQuery
object
2. $("virtual-sensor", data).get( i )
get( i ) : From the object returned by (1) above, get just the
element at index position i (eg. if 'i' is 0 - first time through loop
- then it's the first element), returning it *as an element*
3. $( $("virtual-sensor", data).get( i ) )
$( element ) : Create a jQuery object of the element returned by
(2) above
4. $( $("virtual-sensor", data).get( i ) ).attr("name")
attr( attribute ) : From the jQuery object returned by (3) above,
return the value of the 'name' attribute that belongs to the element
retrieved in (2) above.
5. var n = $( $("virtual-sensor", data).get( i ) ).attr("name");
Assign the returned string (or null) from (4) to the variable n
So first time through the loop, with i = 0, you get...
1. jQuery object containing ALL 'virtual-sensor' elements
2. first element of collection (ie the one with name =
templatevirtualsensor)
3. jQuery object containing JUST that first element
4. value of the name attribute from that first element, eg
"templatevirtualsensor"
5. var n = "templatevirtualsensor"
Second time through the loop, with i = 1, you get...
1. jQuery object containing ALL 'virtual-sensor' elements (again!)
2. second element of collection (ie the one with name = testsvg)
3. jQuery object containing JUST that second element
4. value of the name attribute from that second element, eg "testsvg"
5. var n = "testsvg";
etc, etc, through to the 10th element.
As Karl has already pointed out, the way the code currently converts
from a jQuery object into an element, then back into a jQuery object,
is a waste of effort.
The code could be better optimised to (for example) ...
// just get the full collection once
var virtualSensors = $("virtual-sensor", data);
var n;
for (var i = 0; i < 10; ++i)
{
// from the full collection just get the name attribute of the i
element...
n = virtualSensors.slice(i, i+1).attr("name");
// now do something with it...
if (n!=null) gsn.vsbox.add(n); // add the selection to a box
}
HTH
On Oct 16, 8:46 pm, charliend <[EMAIL PROTECTED]> wrote:
> Thank you very much for our answers.
> Here is the exact algorithm:
> for (var i = 0; i < 10; ++i)
> {
> var n =
> $($("virtual-sensor",data).get(i)).attr("name");
> if (n!=null) gsn.vsbox.add(n); // add the selection
> to a box
> }
> Knowing that data is a xml object and virtual-sensor an id of this
> data.
> Here is an example of the variable data:
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <gsn name="GSN Server" author="My name" email="my email address"
> description="Not Provided">
> <virtual-sensor name="templatevirtualsensor" last-
> modified="1191537808156" description="Not Specified, please edit the
> file $INSTALL_PATH/virtual-sensors/template.xml.">
> </virtual-sensor>
> <virtual-sensor name="testsvg" last-modified="1191537808140"
> description="Not Specified, please edit the file $INSTALL_PATH/virtual-
> sensors/networkPlotter.xml">
> <field name="plot" type="binary:image/svg+xml">/field?
> vs=TESTSVG&field=PLOT&pk=66321</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563879750</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/networkPlotter.xml</field>
> <field name="plot" type="binary:image/svg+xml">/field?
> vs=TESTSVG&field=PLOT&pk=66322</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563879750</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/networkPlotter.xml</field>
> </virtual-sensor>
> <virtual-sensor name="localsystemtime" last-modified="1191537808125"
> description="Not Specified, please edit the file $INSTALL_PATH/virtual-
> sensors/localTimeVS.xml.">
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563879265</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/localTimeVS.xml.</field>
> </virtual-sensor>
> <virtual-sensor name="memoryplotvs" last-modified="1191537808125"
> description="Not Specified, please edit the file $INSTALL_PATH/virtual-
> sensors/memoryPlotVS.xml">
> <field name="data" type="binary:image/jpeg">/field?
> vs=MEMORYPLOTVS&field=DATA&pk=5367</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563876812</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/memoryPlotVS.xml</field>
> </virtual-sensor>
> <virtual-sensor name="gpsvs" last-modified="1191537808140"
> description="Virtual sensor producing random locations and sensor
> readings for testing in GPS applications.">
> <field name="latitude" type="double" description="tested
> desc">255.9119</field>
> <field name="longitude" type="double">-122.1419</field>
> <field name="temperature" type="double">25.5</field>
> <field name="light" type="int">650</field>
> <field name="camera" type="binary:image/jpeg">/field?
> vs=GPSVS&field=CAMERA&pk=21846</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563879187</field>
> <field name="type" category="predicate">test-sensor</field>
> <field command="ploppy" name="plop1" category="input"
> type="radio:apple|orange|banana" description="one"></field>
> <field command="ploppy" name="plop2" category="input"
> type="*checkbox:apple|orange|banana" description="two"></field>
> <field command="ploppy" name="plop3" category="input"
> type="select:apple|orange|banana" description="three"></field>
> <field command="test" name="reset1" category="input" type="byte"></
> field>
> <field command="test" name="reset2" category="input" type="*binary:
> 2mb" description="Files Size of 2MB max, The * means optional."></
> field>
> </virtual-sensor>
> <virtual-sensor name="remoteaccessdemo" last-modified="1191537808156"
> description="Not Specified, please edit the file $INSTALL_PATH/virtual-
> sensors/remoteTest.xml">
> <field name="data" type="binary:image/jpeg">/field?
> vs=REMOTEACCESSDEMO&field=DATA&pk=5367</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563876812</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/remoteTest.xml</field>
> </virtual-sensor>
> <virtual-sensor name="gps2" last-modified="1191537808156"
> description="Not Specified, please edit the file $INSTALL_PATH/virtual-
> sensors/localTimeVS.xml.">
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563879265</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/localTimeVS.xml.</field>
> <field name="latitude" category="predicate">37.4</field>
> <field name="longitude" category="predicate">-122.1</field>
> </virtual-sensor>
> <virtual-sensor name="memorymonitorvs" last-modified="1191537808156"
> description="Not Specified, please edit the file $INSTALL_PATH/virtual-
> sensors/memoryDataVS.xml">
> <field name="heap" type="bigint">11202712</field>
> <field name="non_heap" type="bigint">25021096</field>
> <field name="pending_finalization_count" type="integer">0</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563879828</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file
> $INSTALL_PATH/virtual-sensors/memoryDataVS.xml</field>
> <field name="latitude" category="predicate">37.5419</field>
> <field name="longitude" category="predicate">-122.1419</field>
> </virtual-sensor>
> <virtual-sensor name="invmaccess" last-modified="1191537808140"
> description="Not Specified, please edit the file $INSTALL_PATH/virtual-
> sensors/remoteTest.xml">
> <field name="data" type="binary:image/jpeg">/field?
> vs=INVMACCESS&field=DATA&pk=5367</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563876812</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/remoteTest.xml</field>
> </virtual-sensor>
> <virtual-sensor name="multiformattemperaturehandler" last-
> modified="1191537808140" description="Presents how you can have a
> virtual sensor handling and parsing only a specific groups of packets
> produced by a wrapper.">
> <field name="temperature" type="double">66.8</field>
> <field name="timed" type="long" description="The timestamp associated
> with the stream element">1192563878765</field>
> <field name="geographical" category="predicate">Not Specified, please
> edit the file $INSTALL_PATH/virtual-sensors/localTimeVS.xml.</field>
> </virtual-sensor>
> </gsn>
>
> I'm still not sure on what it does... But I know that it works.
>
> On 16 oct, 18:52, Karl Swedberg <[EMAIL PROTECTED]> wrote:
>
> > On Oct 16, 2007, at 12:24 PM, Andy Matthews wrote:
>
> > > // creates a new, empty jQueru object
> > > $(
> > > // create a new jQuery object using virtual-sensor
> > > // as the selector, with 'data' as the context
> > > // and it returns the ith record in that query into
> > > // an array.
> > > $("virtual-sensor",data).get(i)
> > > // this gets all records that have an attribute of name
> > > ).attr("name");
>
> > A couple notes:
> > 1. It looks like the selector is converting a jQuery object into a
> > DOM element and then back into a jQuery object. Seems a little
> > convoluted. I think the same thing can be achieved this way (assuming
> > that "i" is coming from somewhere earlier in the script):
>
> > $('virtual-sensor:eq(' + i + ')', data).attr('name');
>
> > 2. The attr('name') part is actually returning the value of the name
> > attribute. It's not filtering the matched set of elements.
>
> > > The problem is that this code will never work as written. The
> > > author is
> > > trying to
> > > Use 'virtual-sensor' as a selector, but in this setup, they're
> > > acting as if
> > > it's an HTML tag, which it's not. It should be '.virtual-sensor' or
> > > '#virtual-sensor'
>
> > Actually, if this is an XML file, it's quite possible that there is a
> > <virtual-sensor> element.
>
> > --Karl
>
> > > -----Original Message-----
> > > From: [email protected] [mailto:jquery-
> > > [EMAIL PROTECTED] On
> > > Behalf Of charliend
> > > Sent: Tuesday, October 16, 2007 9:05 AM
> > > To: jQuery (English)
> > > Subject: [jQuery] Can't understand a Jquery statement
>
> > > Hello all,
>
> > > I'm beginning working on a project which has been writing using
> > > Jquery (I'm
> > > novice) and I really can't understand that line:
> > > $($("virtual-sensor",data).get(i)).attr("name");
>
> > > Can you traduce it to me?
> > > Thank you
> > > Charlie