Static content before loop - xquery

Am new to XQuery. I have written below query which is working perfectly and producing results in csv format from an
xml stored in Marklogic.
xquery version "0.9-ml"
let $data := someQuery
return
for $i in $data
return
fn:string-join((
$i/PATHTOFILED_1/text(),
$i/PATHTOFILED_2/text(),
.
.
.
.
$i/PATHTOFILED_N/text()
),","
)
Output:
abc,def,adc,
dff,eef,ddf,
.
.
.
.
fff,eed,ddg,
I have a new requirement to add static headers before the data.
And the expected output will be like
Expected Output:
HEAD1, HEAD2, HEAD3,
abc,def,adc,
dff,eef,ddf,
.
.
.
.
fff,eed,ddg,
Just adding HEAD1, HEAD2, HEAD3, in the top row. The headers are not part of the XML. They should be part of query in simple Strings with static data and can be modified at anytime in query itself.
I have tried to add the below code segment to the query. But the query is not running in Marklogic query console.
concat("HEAD1,"HEAD2","HEAD3"),
Any solution is highly appreciated.
Thanks in advance.

Add it before the FLWOR expression:
xquery version "1.0-ml";
string-join(("HEAD1","HEAD2","HEAD3"),","),
let $data := someQuery
return
for $i in $data
return
fn:string-join((
$i/PATHTOFILED_1/text(),
$i/PATHTOFILED_2/text(),
.
.
.
.
$i/PATHTOFILED_N/text()
),","
)
BTW, I really wouldn't use to 0.9-ml dialect unless you have a very compelling reason to do so. It works, but it is really only still there for compatibility.

Related

Why does SPARQL query take 30x as long in R package than on Land Registry?

When I run the following SPARQL query on the Land Registry console it takes c.0.4 seconds and returns all 2599 results:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix ukhpi: <http://landregistry.data.gov.uk/def/ukhpi/>
SELECT
?stripped_regionName ?stripped_date ?ukhpi ?avprice ?volume ?newbuildvolume ?regionName ?regionId ?region
WHERE
{
VALUES ?regionId {<http://landregistry.data.gov.uk/id/region/southampton> <http://landregistry.data.gov.uk/id/region/london> <http://landregistry.data.gov.uk/id/region/england> <http://landregistry.data.gov.uk/id/region/wales> <http://landregistry.data.gov.uk/id/region/scotland> <http://landregistry.data.gov.uk/id/region/barking> <http://landregistry.data.gov.uk/id/region/southwark> <http://landregistry.data.gov.uk/id/region/westminster> <http://landregistry.data.gov.uk/id/region/merton> <http://landregistry.data.gov.uk/id/region/greenwich> <http://landregistry.data.gov.uk/id/region/camden>}
?region ukhpi:refRegion ?regionId .
?region ukhpi:refMonth ?date .
?region ukhpi:housePriceIndex ?ukhpi .
?region ukhpi:averagePrice ?avprice .
?region ukhpi:salesVolume ?volume .
?region ukhpi:salesVolumeNewBuild ?newbuildvolume .
?regionId rdfs:label ?regionName
FILTER (langMatches( lang(?regionName), "EN") ) .
BIND (STR(?regionName) AS ?stripped_regionName) .
BIND (STR(?date) AS ?stripped_date) .
}
When I run the same query using the SPARQL package for R, it takes c.15.0 seconds to return all 2599 results:
endpoint <- "https://landregistry.data.gov.uk/landregistry/query"
query <- '
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix ukhpi: <http://landregistry.data.gov.uk/def/ukhpi/>
SELECT
?stripped_regionName ?stripped_date ?ukhpi ?avprice ?volume ?newbuildvolume ?regionName ?regionId ?region
WHERE
{
VALUES ?regionId {<http://landregistry.data.gov.uk/id/region/southampton> <http://landregistry.data.gov.uk/id/region/london> <http://landregistry.data.gov.uk/id/region/england> <http://landregistry.data.gov.uk/id/region/wales> <http://landregistry.data.gov.uk/id/region/scotland> <http://landregistry.data.gov.uk/id/region/barking> <http://landregistry.data.gov.uk/id/region/southwark> <http://landregistry.data.gov.uk/id/region/westminster> <http://landregistry.data.gov.uk/id/region/merton> <http://landregistry.data.gov.uk/id/region/greenwich> <http://landregistry.data.gov.uk/id/region/camden>}
?region ukhpi:refRegion ?regionId .
?region ukhpi:refMonth ?date .
?region ukhpi:housePriceIndex ?ukhpi .
?region ukhpi:averagePrice ?avprice .
?region ukhpi:salesVolume ?volume .
?region ukhpi:salesVolumeNewBuild ?newbuildvolume .
?regionId rdfs:label ?regionName
FILTER (langMatches( lang(?regionName), "EN") ) .
BIND (STR(?regionName) AS ?stripped_regionName) .
BIND (STR(?date) AS ?stripped_date) .
}'
qd <- SPARQL(endpoint, query)
hpi_df <- qd$results
Is there a way to speed up the query when run through R, or is the delay unavoidable? I'm hoping there is a fix, but imagine it could be because the Land Registry console is always connected, but my R query needs to connect to the server first.
Just to answer this one, as far as I can see it does appear to be the R package that is slow.
My way around it was to download all the data and pre-load this, then create a function to only query and add new data each time someone loads up the app.
Filtering the data returned by the query to the minimum fields needed also helped.

PHPExcel row break not working

. . . .
I am using Codeigniter 3.1 and PHPExcel 1.8.
I have a function that creates a PHPExcel Object and returns it and the other function outputs the Excel to browser
Everything is working perfectly fine. Now on specific rows I need to add page breaks.
if($count == 4 || ($count > 4 && (($count - 4) % 6 == 0))){
//echo("A - $row <br>Count - $count<br><hr>");
$sheet->setBreak('A' . $row , PHPExcel_Worksheet::BREAK_ROW );
}
The echo is giving me my required rows, so the condition is working fine. The only issues is . . . . . page break not working. So any suggestions?
Following is the code used for generating the file
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file_name");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5");
$objWriter->save("php://output");
Problem solved. Initially I was using the setbreak function while inserting rows. and whenever the condition meets, call the function. I was going through the code, got an idea, implemented it and solved. Instead of calling the function setbreak during row generation, i stored the row reference in an array, and then after doing all page settings, at the end looped the array and called setbreak on the rows and it worked :). Thanks Mark as discussing with you has helped me twice now :)

Multiple query are not inserting data Wordpress

I wrote a function where two insert query have. One is executed and inserting data properly. But next one is not executing. And I cant check the value i want to insert if it is set or not. How to do the stuff? EXPERT's have a look kindly. My function is given below:
add_action( 'save_post', 'cs_product_save' );
function cs_product_save( $post_id ){
global $wpdb;
$cs_product_array = $_POST['cs_product'];
$cs_product_count = count($cs_product_array);
$event_start_date = $_POST['event_start_date'];
$event_end_date = $_POST['event_end_date'];
$event_start_time = $_POST['event_start_time'];
$event_end_time = $_POST['event_end_time'];
$event_all_day = $_POST['event_all_day'];
$event_phone = $_POST['event_phone'];
$event_location = $_POST['event_location'];
$event_map = $_POST['event_map'];
$table_cause_product = "wp_cause_woocommerce_product";
$table_event_info = "wp_cause_info";
for( $i=0; $i < $cs_product_count; $i++ ){
$wpdb->insert($table_cause_product,array(
'cause_ID'=>$post_id,
'product_ID'=>$cs_product_array[$i],
'status'=>'1'
),array('%d','%d','%d'));
}
$wpdb->insert($table_event_info,array(
'cause_ID'=>$post_id,
'event_start_date'=>$event_start_date,
'event_end_date'=>$event_end_date,
'event_start_time'=>$event_start_time,
'event_end_time'=>$event_end_time,
'event_all_day'=>$event_all_day,
'event_phone'=>$event_phone,
'event_location'=>$event_location,
'event_map'=>$event_map
),array('%d','%s','%s','%s','%s','%d','%s','%s','%d'));
}
I don't see any problem here with your code. But be sure to double check your code.
The issues my be with the name of your database table names. Are you sure that $table_cause_product and $table_event_info holds the actual name of the tables? I would recommend to use $wpdb->prefix instead of harcoding table names.
In my case I would check the function in several parts.
Check the $_POST actually holds the data I want.
Use $result = $wpdb->insert( $table, $data, $format ); in all cases for debug purpose as the $result would hold the result of the operation. If its is false then I would be sure that there is definitely something wrong with the operation.
Finally I would use a wp_die() (though its not a standard way to do, but it suffices my purpose) so that I can see the dumped variable data.
One major issue you might face with your code that if the post is edited after saving then it might insert another row for same post data. Again if the post is being autosaved, you need some safeguard. I would recommend a where clause here to check the row already exists or not. If exists then you can simply update the row, or else insert the data.
Hope this might help you.

mysql_fetch_row loop?

I set a session variable upon login, I want to find all rows in a table that have the username in the "Createdby" field and I want to list them on a page. I'm using this code:
<?php
$result = mysql_query("SELECT email FROM members WHERE createdby = '" . $_SESSION['myusername'] ."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_row($result))
{
echo ($row[0]);
}
?>
It works great but it doesn't space them, it echoes out like: data1data2 and not separate like data1, data2. How can I customize the results without messing it up? I tried to add
echo ("<p>".$row[0]."</p>");
But received: 11, I'm kind of new to PHP.
This is simply string concatenation. You're adding strings to other strings.
I'd suggest the following just to get started:
echo $row[0].", ";
I also suggest you read up on PHP more, as this is a basic concept in the language.
You can add spaces between each $row[0] like this:
echo $row[0] . ' ';
The dot followed by a space means that you're concatenating a space.

Query Posts issue in wordpress

I am a little unsure how to do a WP_Query to search a meta value of my posts. The meta data includes a persons height. So valid entries would be 5'9" or 4'11" and I need to do a Compare-Between.
To make it a bit clearer: I have a filter page. So the user can select the height between 4'0" - 5'9 or 5'9" - 5'11"
The problem is I have the ' and " symbols. I can remove these. But then it will be searching between 59 and 511 which is not going to work.
Anyone know of a work around?
Save the values in inches.
You could then set up a WP_Query or a $wpdb query to fetch your results.
Whenever you are displaying the values on the front end, you convert them back to feet and inches.
update:
function convert_to_feet( $input){
$feet = (int) ($input/12);
$inches = $input%12;
return $feet . "'" . $inches . '"';
}
echo convert_to_feet(71);

Resources