I've tried with a number of variations. As far as a self-contained example,
I'm not exactly sure what you're looking for, but suppose I have in
C:\data\input.xml:
<?xml version="1.0" encoding="UTF-8"?>
<input>
<pages>
<page>
<lbl>Sample Page</lbl>
<list>
<unlt>
<lbl>Summer Unit</lbl>
<partinfo>
<number>54321</number>
<manuf>A321</manuf>
</partinfo>
<partinfo>
<number>12345</number>
<manuf>B123</manuf>
</partinfo>
<offer>25% off!</offer>
</unlt>
</list>
</page>
</pages>
<parts>
<part>
<partinfo>
<number>54321</number>
<manuf>A321</manuf>
</partinfo>
<color>Orange</color>
</part>
<part>
<partinfo>
<number>12345</number>
<manuf>B123</manuf>
</partinfo>
<color>Pink</color>
</part>
</parts>
</input>
And suppose I have in C:\scripts\query.xq:
let $map := map:merge(
for $part in //part
return map:entry(string-join($part/partinfo/*, '/'), $part)
)
for $page in //page
let $new-part := $page update {
for $partinfo in .//unit/partinfo
let $part := $map(string-join($partinfo/*, '/'))
return replace node $partinfo with element part {$part/node()}
}
return file:write('/data/'|| $page/@key || '.xml', $new-part,
map{"omit-xml-declaration":"no"})
And suppose my current directory is C:\scripts. If I launch the command:
"\Program Files (x86)\BaseX\bin\basex" query.xq -iC:/data/input.xml
I get in response:
Stopped at C:/scripts/query.xq, 2/19:
[XPDY0002] root(): no context value bound.
It's the same whether I say
-iC:/data/input.xml
-i/data/input.xml
-iC:\data\input.xml
-i\data\input.xml
Thanks,
Michael
On Mon, May 30, 2016 at 2:20 PM, Christian Grün <[email protected]>
wrote:
> > If I just remove "$input"
> > from the script I get a "no context value bound" error if I say
> >
> > -i/my/path/to/input.xml
>
> It should work as described (I frequently use it by myself). Could you
> try again, or provide me with a little self-contained example?
>
> Cheers,
> Christian
>
>
>
> > On Thu, May 26, 2016 at 11:21 PM, Christian Grün <
> [email protected]>
> > wrote:
> >>
> >> Hi Michael,
> >>
> >> > 1) There'll be a new version of the input file arriving periodically,
> so
> >> > I'd
> >> > prefer to be able to do everything at the command line without having
> to
> >> > create the database inside the GUI. Not sure of the best way to go
> about
> >> > that.
> >>
> >> Using the GUI was just an example. You can create new databases via
> >> commands (CREATE), XQuery (db:create) or our APIs. Please check out
> >> our documentation for more hints.
> >>
> >> > 2) In the GUI for now, I can perform the replace with the map, or I
> can
> >> > run
> >> > the for loop that writes out all the files. But I get an "Unexpected
> end
> >> > of
> >> > query" error when I try to do both, so what's the problem with my
> >> > syntax?
> >>
> >> In XQuery, multiple expressions can be separated with commas.
> >>
> >> Note, however, that XQuery is a functional language; as such, it is
> >> not possible to first update items and then access them in the same
> >> query that easily. There are various alternatives to get around this
> >> limitation:
> >>
> >> 1. Use a BaseX command script to run all operations [1]:
> >>
> >> <commands>
> >> <create-db name='input'>...path/to/input.xml</create-db>
> >> <xquery><![CDATA[
> >> let $map := ...
> >> ]]></xquery>
> >> <xquery>
> >> for $page in ...
> >> <xquery>
> >> </commands>
> >>
> >> 2. Use copy/transform/return or update to do all updates in
> >> main-memory [2] and pass them on to your file:write function. This
> >> could e.g. look as follows:
> >>
> >> let $input := doc('input.xml')
> >> let $map := map:merge(
> >> for $part in $input//part
> >> return map:entry(string-join($part/partinfo/*, '/'), $part)
> >> )
> >> for $page in $input//page
> >> let $new-part := $page update {
> >> for $partinfo in .//unit/partinfo
> >> let $part := $map(string-join($partinfo/*, '/'))
> >> return replace node $partinfo with element part { $part/node() }
> >> }
> >> return file:write('data/'|| $page/@key || '.xml', $page,
> >> map{ "omit-xml-declaration": "no" })
> >>
> >> The good thing here is that your replace operations won’t need to be
> >> cached until the very end; they will directly be run on each page
> >> element (and not persisted on disk; but this is something you don’t
> >> need anyway it seems)
> >>
> >> Christian
> >>
> >> [1] http://docs.basex.org/wiki/Commands#Command_Scripts
> >> [2] http://docs.basex.org/wiki/XQuery_Update#Non-Updating_Expressions
> >
> >
>