Example xquery needed to write output to an local xml file
Thursday, 6 September 2018 · 6 messages
MR
Marco Roling6 Sep 2018 17:13 #
Dear all,
I am new on the block here, and trying to find my way around BaseX, which seems to meet my needs for my research on digital museum collection data analysis.
I have a very large collection of xml files (> 600000 files) that I am querying, and I want to write the returned result immediately in a file. The following xquery (see below) is not working, which probably comes as no surprise to you all.
But to get me started, I would like to have a simple example that works. Any example would do.
Hope anyone can help me out with this.
Thanks very much, best, Marco.
=====
declare variable $data := collection(PRODRMCollectionItems);
declare variable $output := 'C:\DATAQUERY\BaseX\Q001\outputPROD';
(:This is where all the work is happening:)
declare function local:process-data ($nodes as item()*) as element (root)
{
<RMCollectionItemsDATA created="{current-dateTime()}">
{
for $a in $nodes//artObjectGetResponse/artObject[objectNumber eq 'NG-NM-1395-A']
return
<item>{$a}</item>
}
</RMCollectionItemsDATA>
};
(:here we store stuff:)
let $filename := concat($output, "marco.xml")
return
file:write($filename, local:process-data($data))
=====AB
Andy Bunce6 Sep 2018 17:36 #
Hi Marco,
BaseX sounds like a good fit for your requirements to me :)
You dont say what your error is, but you have declared local:process-data as returning an element named <root> whereas the code is returning an element named <RMCollectionItemsDATA>.
Perhaps it should be:
declare function local:process-data ($nodes as item()*) as element ( RMCollectionItemsDATA )
or
... as element ( * )
Also I suspect you want an additional \ in your o/p path
declare variable $output := 'C:\DATAQUERY\BaseX\Q001\outputPROD*\*';
Regards
/Andy
-----------------------------------
declare variable $data := collection(PRODRMCollectionItems); declare variable $output := 'C:\DATAQUERY\BaseX\Q001\outputPROD*\*';
(:This is where all the work is happening:)
declare function local:process-data ($nodes as item()*) as element (
*RMCollectionItemsDATA*)
{
<RMCollectionItemsDATA created="{current-dateTime()}">
{
for $a in $nodes//artObjectGetResponse/artObject[objectNumber eq
'NG-NM-1395-A']
return
<item>{$a}</item>
}
</RMCollectionItemsDATA>
};
(:here we store stuff:)
let $filename := concat($output, "marco.xml")
return
file:write($filename, local:process-data($data))Quoted text · Marco Roling · 45 lines
On 6 September 2018 at 16:13, Marco Roling <marcoroling at hotmail.com> wrote: > Dear all, > > I am new on the block here, and trying to find my way around BaseX, which > seems to meet my needs for my research on digital museum collection data > analysis. > > I have a very large collection of xml files (> 600000 files) that I am > querying, and I want to write the returned result immediately in a file. The > following xquery (see below) is not working, which probably comes as no > surprise to you all. > > But to get me started, I would like to have a simple example that works. > Any example would do. > > Hope anyone can help me out with this. > > Thanks very much, best, Marco. > > > ===== > > declare variable $data := collection(PRODRMCollectionItems); > declare variable $output := 'C:\DATAQUERY\BaseX\Q001\outputPROD'; > > (:This is where all the work is happening:) > declare function local:process-data ($nodes as item()*) as element (root) > { > <RMCollectionItemsDATA created="{current-dateTime()}"> > { > for $a in $nodes//artObjectGetResponse/artObject[objectNumber eq > 'NG-NM-1395-A'] > return > <item>{$a}</item> > } > </RMCollectionItemsDATA> > }; > > (:here we store stuff:) > let $filename := concat($output, "marco.xml") > return > file:write($filename, local:process-data($data)) > ===== > >
MR
Thanks Bridger,
Great help. I got it working!
Best, Marco.
BD
Hi Marco -
Are you missing a separator ('\') in your $output variable, or in your
`fn:concat($output, '\', 'marco.xml')`?Here's a similar example that's working for me:
declare variable $data := (1, 2, 3, 4);
declare variable $output := '/home/bridger/';
declare function local:process-data(
$seq as item()*
) as item()* {
<test created="{fn:current-dateTime()}">
{
for $s in $data
return(
<item>{$s}</item>
)
}
</test>
};
let $filename := "bridger.xml"
return
file:write($output || $filename, local:process-data($data))I hope this is helpful!
Best,
Bridger
Quoted text · Marco Roling · 46 lines
On Thu, Sep 6, 2018 at 11:13 AM Marco Roling <marcoroling at hotmail.com> wrote: > Dear all, > > I am new on the block here, and trying to find my way around BaseX, which > seems to meet my needs for my research on digital museum collection data > analysis. > > I have a very large collection of xml files (> 600000 files) that I am > querying, and I want to write the returned result immediately in a file. The > following xquery (see below) is not working, which probably comes as no > surprise to you all. > > But to get me started, I would like to have a simple example that works. > Any example would do. > > Hope anyone can help me out with this. > > Thanks very much, best, Marco. > > > ===== > > declare variable $data := collection(PRODRMCollectionItems); > declare variable $output := 'C:\DATAQUERY\BaseX\Q001\outputPROD'; > > (:This is where all the work is happening:) > declare function local:process-data ($nodes as item()*) as element (root) > { > <RMCollectionItemsDATA created="{current-dateTime()}"> > { > for $a in $nodes//artObjectGetResponse/artObject[objectNumber eq > 'NG-NM-1395-A'] > return > <item>{$a}</item> > } > </RMCollectionItemsDATA> > }; > > (:here we store stuff:) > let $filename := concat($output, "marco.xml") > return > file:write($filename, local:process-data($data)) > ===== > >
GS
Graydon Saunders6 Sep 2018 19:41 #
You maybe don't want to start by writing to a file. You want to separate "did the query produce something sensical" and "does the file write do what I expect" as sources of error so you can't try to debug the wrong thing for a few hours.
return
(: file:write($output || $filename, :)
local:process-data($data)
(: ) :)can be a remarkably useful construct until you like what you're seeing. The BaseX GUI very sensibly truncates the output so you can't crush it.
-- Graydon
Quoted text · Bridger Dyson-Smith · 80 lines
On Thu, Sep 6, 2018 at 11:32 AM Bridger Dyson-Smith <bdysonsmith at gmail.com> wrote: > Hi Marco - > Are you missing a separator ('\') in your $output variable, or in your > `fn:concat($output, '\', 'marco.xml')`? > > Here's a similar example that's working for me: > ``` > declare variable $data := (1, 2, 3, 4); > declare variable $output := '/home/bridger/'; > > declare function local:process-data( > $seq as item()* > ) as item()* { > <test created="{fn:current-dateTime()}"> > { > for $s in $data > return( > <item>{$s}</item> > ) > } > </test> > }; > > let $filename := "bridger.xml" > return > file:write($output || $filename, local:process-data($data)) > ``` > > I hope this is helpful! > Best, > Bridger > > On Thu, Sep 6, 2018 at 11:13 AM Marco Roling <marcoroling at hotmail.com> > wrote: > >> Dear all, >> >> I am new on the block here, and trying to find my way around BaseX, which >> seems to meet my needs for my research on digital museum collection data >> analysis. >> >> I have a very large collection of xml files (> 600000 files) that I am >> querying, and I want to write the returned result immediately in a file. The >> following xquery (see below) is not working, which probably comes as no >> surprise to you all. >> >> But to get me started, I would like to have a simple example that works. >> Any example would do. >> >> Hope anyone can help me out with this. >> >> Thanks very much, best, Marco. >> >> >> ===== >> >> declare variable $data := collection(PRODRMCollectionItems); >> declare variable $output := 'C:\DATAQUERY\BaseX\Q001\outputPROD'; >> >> (:This is where all the work is happening:) >> declare function local:process-data ($nodes as item()*) as element (root) >> { >> <RMCollectionItemsDATA created="{current-dateTime()}"> >> { >> for $a in $nodes//artObjectGetResponse/artObject[objectNumber eq >> 'NG-NM-1395-A'] >> return >> <item>{$a}</item> >> } >> </RMCollectionItemsDATA> >> }; >> >> (:here we store stuff:) >> let $filename := concat($output, "marco.xml") >> return >> file:write($filename, local:process-data($data)) >> ===== >> >>
BD
Bridger Dyson-Smith6 Sep 2018 19:56 #
Hi Graydon
Quoted text · Graydon Saunders · 17 lines
On Thu, Sep 6, 2018 at 11:41 AM Graydon Saunders <graydonish at gmail.com> wrote: > You maybe don't want to start by writing to a file. You want to separate > "did the query produce something sensical" and "does the file write do what > I expect" as sources of error so you can't try to debug the wrong thing for > a few hours. > > This is a great point - thanks for the reminder! > return > (: file:write($output || $filename, :) > local:process-data($data) > (: ) :) > > can be a remarkably useful construct until you like what you're seeing. > The BaseX GUI very sensibly truncates the output so you can't crush it. > > And again!
-- Graydon
Quoted text · Graydon Saunders · 2 lines
> > Cheers,
Bridger
Written for BaseX 9.0. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery