how to count the number of elements in XML? - text

I'm looking to get basic metadata about the data within a database. Specifically, the number of line elements which are surrouned by the root text element.
Analogous to what I'd expect COUNT to return in SQL -- a single integer.
the database:
thufir#dur:~/flwor/group$
thufir#dur:~/flwor/group$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
>
> open people
Database 'people' was opened in 225.24 ms.
>
> xquery /
<text>
<line>people</line>
<line>joe</line>
<line>phone1</line>
<line>phone2</line>
<line>phone3</line>
<line>sue</line>
<line>cell4</line>
<line>home5</line>
<line>alice</line>
<line>atrib6</line>
<line>x7</line>
<line>y9</line>
<line>z10</line>
</text>
Query executed in 215.13 ms.
>
> exit
See you.
thufir#dur:~/flwor/group$
Counting the lines:
thufir#dur:~/flwor/group$
thufir#dur:~/flwor/group$ basex each.xq
1
2
3
4
5
6
7
8
9
10
11
12
13thufir#dur:~/flwor/group$
code:
xquery version "3.0";
for $line in db:open("people")
for $index at $count in $line/text/line
return $count

I suppose you simply want to use count(/text/line).

From Martin's answer I see it's easy from within basex itself:
thufir#dur:~/flwor/group$
thufir#dur:~/flwor/group$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
>
> open people
Database 'people' was opened in 208.33 ms.
>
> xquery /
<text>
<line>people</line>
<line>joe</line>
<line>phone1</line>
<line>phone2</line>
<line>phone3</line>
<line>sue</line>
<line>cell4</line>
<line>home5</line>
<line>alice</line>
<line>atrib6</line>
<line>x7</line>
<line>y9</line>
<line>z10</line>
</text>
Query executed in 237.69 ms.
>
> xquery count(/text/line)
13
Query executed in 20.55 ms.
>
> exit
See you.
thufir#dur:~/flwor/group$
but I was also looking to run it from an xq file:
xquery version "3.0";
count(
for $line in db:open("people")
return $line/text/line)

Related

How to Iterate through all the rows of Excel sheet in Robot Framework

How to iterate of all the rows of the Excel sheet. ?
below is my code
*** Settings ***
Library SeleniumLibrary
Library ExcelLibrary
Library Collections
*** Variables ***
${path_excel} C:\\User\\Test.xlsx
*** Test Cases ***
Test1
Test the ExcelData
*** Keywords ***
Test the ExcelData
${my_data_as_list}= Create List
#open the Excel ${path_excel}
open excel document filename=${path_excel} doc_id=Sheet2
FOR ${i} IN 20
${my_data}= Read Excel Cell row_num=${i} col_num=1
Log to Console ${my_data}
Append To List ${my_data_as_list} ${my_data}
Log to Console ${my_data_as_list}
END
it's printing nothing
PS C:\Users\User\PycharmProjects\RobotFramework\Automation> robot .\ExcelExtract.robot
==============================================================================
ExcelExtract
==============================================================================
Test1 None
[None]
Test1 | PASS |
------------------------------------------------------------------------------
ExcelExtract | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Output: C:\Users\User\PycharmProjects\RobotFramework\Automation\output.xml
Log: C:\Users\User\PycharmProjects\RobotFramework\Automation\log.html
Report: C:\Users\User\PycharmProjects\RobotFramework\Automation\report.html
I tried all the possible ways searched online, and was also unable to get the solution
Please change
FOR ${i} IN 20
to
FOR ${i} IN RANGE 1 20

issue with for loops in robot framework

I am new to Robot Framework and I am trying to implement a for loop where I want to loop through and store all the text values of all a tags.
But I am only able to get the text of one single a tag.
This is my code:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${url} http://www.practiceselenium.com/practice-form.html
*** Test Cases ***
ExtractLinks
alllinks
close browser
*** Keywords ***
allLinks
open browser ${url}
${allLinksCount} get element count xpath://a
log to console ${allLinksCount}
FOR ${i} IN 1 ${allLinksCount}
${linkText} get text xpath:(//a)[${i}]
log to console ${linkText}
END
This is the output:
==============================================================================
getAllLinks
==============================================================================
ExtractLinks 10
More
ExtractLinks | PASS |
------------------------------------------------------------------------------
getAllLinks | PASS |
1 test, 1 passed, 0 failed
==============================================================================
So there are 10 a tags on the page, but only one is shown, "More" in the output to the console.
Is it all possible to gather all a link text in list instead?
FOR ${i} IN 1 ${allLinksCount} is equivalent to the python code for i in (1, 10). In other words, it will loop exactly twice.
If you're wanting to iterate over the range of numbers between 1 and ${allLinksCount} you should use IN RANGE.
Because xpath counts starting with one instead of zero, you'll need to adjust the numbers slightly:
FOR ${i} IN RANGE 1 ${allLinksCount}+1
${linkText} get text xpath:(//a)[${i}]
log to console ${linkText}
END
-or-
FOR ${i} IN RANGE ${allLinksCount}
${linkText} get text xpath:(//a)[${i+1}]
log to console ${linkText}
END

rampUser method is getting stuck in gatling 3.3

I am having issues using rampUser() method in my gatling script. The request is getting stuck after the following entry which had passed half way through.
Version : 3.3
================================================================================
2019-12-18 09:51:44 45s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=2 KO=0 )
> graphql / request_0 (OK=1 KO=0 )
> rest / request_0 (OK=1 KO=0 )
---- xxxSimulation ---------------------------------------------------
[##################################### ] 50%
waiting: 1 / active: 0 / done: 1
================================================================================
I am seeing the following in the log which gets repeated for ever and the log size increases
09:35:46.495 [GatlingSystem-akka.actor.default-dispatcher-2] DEBUG io.gatling.core.controller.inject.open.OpenWorkload - Injecting 0 users in scenario xxSimulation, continue=true
09:35:47.494 [GatlingSystem-akka.actor.default-dispatcher-6] DEBUG io.gatling.core.controller.inject.open.OpenWorkload - Injecting 0 users in scenario xxSimulation, continue=true
The above issue is happening only with rampUser and not happening with
atOnceUsers()
rampUsersPerSec()
rampConcurrentUsers()
constantConcurrentUsers()
constantUsersPerSec()
incrementUsersPerSec()
Is there a way to mimic rampUser() in some other way or is there a solution for this.
My code is very minimal
setUp(
scenarioBuilder.inject(
rampUsers(2).during(1 minutes)
)
).protocols(protocolBuilder)
I am stuck with this for some time and my earlier post with more information can be found here
Can any of the gatling experts help me on this?
Thanks for looking into it.
It seems you have slightly incorrect syntax for a rampUsers. You should try remove a . before during.
I have in my own script this code and it works fine:
setUp(userScenario.inject(
// atOnceUsers(4),
rampUsers(24) during (1 seconds))
).protocols(httpProtocol)
Also, in Gatling documentation example is also without a dot Open model:
scn.inject(
nothingFor(4 seconds), // 1
atOnceUsers(10), // 2
rampUsers(10) during (5 seconds), // HERE
constantUsersPerSec(20) during (15 seconds), // 4
constantUsersPerSec(20) during (15 seconds) randomized, // 5
rampUsersPerSec(10) to 20 during (10 minutes), // 6
rampUsersPerSec(10) to 20 during (10 minutes) randomized, // 7
heavisideUsers(1000) during (20 seconds) // 8
).protocols(httpProtocol)
)
My guess is that syntax can't be parsed, so instead 0 is substituted. (Here is example of rounding. Not applicable, but as reference: gatling-user-injection-constantuserspersec)
Also, you mentioned that others method work, could you paste working code as well?

Bash select random string from list [duplicate]

This question already has answers here:
How to select a random item from an array in shell
(5 answers)
Closed 4 years ago.
I have a list of strings that I want to select one of them at random each time I launch the script.
For example
SDF_BCH_CB="file1.sdf"
SDF_BCH_CW="file2.sdf"
SDF_BCH_RCB="file3.sdf"
SDF_BCH_RCW="file4.sdf"
SDF_TT="file5.sdf"
Then I want to be randomly select from the above list to assign the following two variables.
SDFFILE_MIN=$SDF_BCH_CW
SDFFILE_MAX=$SDF_TT
How can I do this ?
Thanks
Store in array, count and random select:
#!/bin/bash
array[0]="file1.sdf"
array[1]="file2.sdf"
array[2]="file3.sdf"
array[3]="file4.sdf"
size=${#array[#]}
index=$(($RANDOM % $size))
echo ${array[$index]}
Use the built in $RANDOM function and arrays.
DF_BCH_CB="file1.sdf"
SDF_BCH_CW="file2.sdf"
SDF_BCH_RCB="file3.sdf"
SDF_BCH_RCW="file4.sdf"
SDF_TT="file5.sdf"
ARRAY=($DF_BCH_CB $SDF_BCH_CW $SDF_BCH_RCB $SDF_BCH_RCW $SDF_TT)
INDEX=(0 1 2 3 4)
N1=$((RANDOM % 5))
SDFFILE_MIN=${ARRAY[$N1]}
N2=$((RANDOM % 4))
if [ "$N2" = "$N1" ] ; then
N2=$N1+1
fi
SDFFILE_MAX=${ARRAY[$N2]}
echo $SDFFILE_MIN
echo $SDFFILE_MAX
This gets two different strings for SDFFILE_MIN and SDFFILE_MAX. If they don't have to be different, remove the if statement in the middle.

How to change the default quantity of lines visible in debug mode?

It shows only 2 lines before and after the current statement. I would like to change the quantity of lines each time I press n (next) or c (continue). The function list(n), quoting the documentation, it says:
list(5) - List scripts source code with 5 line context (5 lines before
and after)
Debug Info - NodeJS
For example executing:
$: node debug app.js
break in c:\nodejs\app.js:3
1
2
> 3 var fs= require('fs');
4
5 console.log('Hello World!');
debug>
I want to change the default number each time I move on statements. Or, an opinion which would be the best way to do it ?

Resources