about xi:include and xml:base
Thursday, 1 October 2015 · 2 messages
MR
Marco Randazzo1 Oct 2015 00:26 #
In my application I have a main xml file (general.xml) which includes several xml files (file1.xml, file2.xml...) using the xi:include tag. I successfully imported general.xml in basex using the gui: the xi:include directives were successfully processed, merging all data in a single database. I noticed in the result window that the database keeps track of the origin of the data, adding an xml:base info for each included block. I then performed some find and replace operation on the database, modifying some values which were originally belonging to different files. Now I want to export data to file. I successfully obtained a single xml file using the export command of the gui. The generated file still preserves the xml:base infos. However, I'd like to recreate the original structure, i.e. obtain multiple files, the same which were merged by xi:include directives at the beginning of the import process. I cannot find such an option in the export tab. Is it possible?
Thank you for the help,
MR
CG
Christian Grün1 Oct 2015 09:41 #
Hi Marco,
There is currently no way to do this automatically. There are various reasons for that (e.g.: what is going to happen if the URIs in the href attributes point to remote sources? etc.). What you can always do is to write your own exporter in XQuery! It may take some time, but it gives you full flexibility. I have attached a simple solution that should work for simple includes. You may need to extend it if the URIs are not simple file names, or if the xi:include attributes are nested (a.xml includes b.xml includes c.xml..).
Hope this helps,
Christian
declare namespace xi = "http://www.w3.org/2001/XInclude";
declare variable $DATABASE := 'name-of-db';
declare variable $TARGET-PATH := 'export/';
file:create-dir($TARGET-PATH),
for $doc in db:open($DATABASE)
let $normalized := $doc update (
for $node in .//*
let $uri := $node/@xml:base/string()
where $uri
let $export := $node update delete node @xml:base
return (
replace node $node with <xi:include href="{ $uri }"/>,
file:write($TARGET-PATH || $uri, $export)
)
)
let $path := $TARGET-PATH || db:path($doc)
return file:write($path, $normalized)Quoted text · Marco Randazzo · 21 lines
On Thu, Oct 1, 2015 at 12:26 AM, Marco Randazzo <Marco.Randazzo at iit.it> wrote: > In my application I have a main xml file (general.xml) which includes > several xml files (file1.xml, file2.xml…) using the xi:include tag. I > successfully imported general.xml in basex using the gui: the xi:include > directives were successfully processed, merging all data in a single > database. I noticed in the result window that the database keeps track of > the origin of the data, adding an xml:base info for each included block. > > I then performed some find and replace operation on the database, modifying > some values which were originally belonging to different files. Now I want > to export data to file. > > I successfully obtained a single xml file using the export command of the > gui. The generated file still preserves the xml:base infos. However, I’d > like to recreate the original structure, i.e. obtain multiple files, the > same which were merged by xi:include directives at the beginning of the > import process. I cannot find such an option in the export tab. Is it > possible? > > Thank you for the help, > MR
Written for BaseX 8.3. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery