Writing output to multiple files

Thursday, 1 December 2011 · 2 messages
AK
Andreas Karpf1 Dec 2011 18:53 #
Dear BaseX Team! After several unfortunately unsuccessful trials & errors I am appealing to with a question which should be easy for you to answer, as I think it is pretty basic. I have a BaseX database filled up with thousands of different but equally structured xml-documents. What I want to do is to parse out necessary information and transform that into html files with distinct names. Actually I can parse out all the information I want but the output at the moment is just one huge html-file. The corresponding xquery looks something like this (I just left the parts in there which seem in my opinion relevant for my question, for real the whole xquery files is much much longer)
declare option output:method "html";

(:Define word count function:)
declare namespace functx = "http://www.functx.com";
declare function functx:word-count ($arg as xs:string?) as xs:integer
{count(tokenize($arg,'\W+')[. != ''])};

let $items:=
for $docitem in .//docitem
where

(:Text length (number of tokens):)
for $text in $docitem/text
where functx:word-count($text)<250
return

(:Language :)
for $language in $docitem/@xml:lang
where $language="en"
return

<html>
<body>
<h3>{data($docitem/headline/text())}</h3>
{$docitem/text/p/text()}
<br>ID: {data($docitem/@itemid)}</br>
<br>Word count:{functx:word-count($text)}</br>
</body>
</html>
return file:write("//htmlfolder/file.html", $items) At the moment I just get all the parsed out elements in one if also well structured html file. What I however want would be multiple html files as output which have a filename like "itemid.htm" from $docitem/@itemid which is a distinct and exact identifier for all the xml files in the database. how can I achieve that. I have tried it with something like the following, but it won't work and I don't arrive to figue it out why:
let $docitem:= .//docitem
let $doc_id := data($docitem/@itemid)

let $documentname := concat('//htmlfolder/', $doc_id, '.html')
return file:write($documentname, $items) BaseX always tells me that "no sequences are allowed regarding $docitem/@itemid". What could be the problem there, or how could I solve this problem. I have tried several combinations because from what I have found on the web regarding my problem the solution should go into a similar direction. Would please somebody of you be so nice to help me with that. Every hint would be very much appreciated! Best regards, Andreas
CG
Christian Grün1 Dec 2011 19:09 #
Hi Andreas,
Quoted text · Andreas Karpf · 11 lines
> let $docitem:= .//docitem > let $doc_id := data($docitem/@itemid) > let $documentname := concat('//htmlfolder/', $doc_id, '.html') > return file:write($documentname, $items) > > BaseX always tells me that "no sequences are allowed > regarding $docitem/@itemid". What could be the problem there, or how could I > solve this problem. I have tried several combinations because from what I > have found on the web regarding my problem the solution should go into a > similar direction. Would please somebody of you be so nice to help me with > that. Every hint would be very much appreciated!
If I got it right, you need to loop through all single items instead of binding them only once. This can be done by replacing "let" with "for". An Example:
  for $docitem in (<a itemid='a'/>, <b itemid='b'/>)
  let $doc_id := data($docitem/@itemid)
  let $documentname := concat('//htmlfolder/', $doc_id, '.html')
  return $documentname
If I didn't get the point, feel free to ask again, Christian

⚡Generated with XQuery