PDA

View Full Version : Number of nodes


Blackdalhia
10-20-2009, 12:13 AM
Hi

i've got a dataset with descriptions of item like :
<item idItem="...">
<Option id="1" name="option 1" .../>
<Option id="2" name="option 1" .../>
<Description id="1" name="Description 1" .../>
<Description id="2" name="Description 2" .../>
<Description id="3" name="Description 3" .../>
</item>

the number of Option and Description record are variable from one item to another : how do i simply know the number of Option or number of Description records ?

Markus
10-20-2009, 11:19 AM
Normally you'd use the XPath count() function but if I recall this doesn't work in the Openlaszo xpath implementation. But you can script the solution with a datapointer and xpathQuery.

Below is some example code. Doing this without a laszlo compiler on-hand so can't test it.


<dataset name="dsMyData">
<item idItem="...">
<Option />
<Option />
<Description />
</item>
</dataset>

<script><![CDATA[
var dp = dsMyData.getPointer();

// This will return an array containing both options.
// Check the datapointer.xpathQuery function for details
//
var options = dp.xpathQuery( "dsMyData:/item/Option" );
Debug.write( "Nr of options:", options.length );

// If only 1 result can be found this function return an LzDataElement
// As an example I'll write code expection 0, 1 or more as a result
//
var descriptions = dp.xpathQuery( "dsMyData:/item/Description" );

var count = 0;
if ( descriptions is LzDataElement ) // Or lz.DataElement in newer Laszlo
{
count = 1;
}
else if ( null != descriptions )
{
count = descriptions.length
}
Debug.write( "Nr of descriptions:", count );

dp.destroy();
]]></script>


You could run a script in an ondata handler for example and assign the count to an attribute somewhere. You could then write constraints on that attribute if you want. You would need to rerun the count script every time the data is updated.

Hope this example helps :)

Blackdalhia
10-21-2009, 10:06 PM
thx Markus, i'll try this, but it looks very good solution to me !