ebeshero / DHClass-Hub

a repository to help introduce and orient students to the GitHub collaboration environment, and to support DH classes.
GNU Affero General Public License v3.0
27 stars 27 forks source link

Creating Plain Text Files with XQuery #636

Closed frabbitry closed 5 years ago

frabbitry commented 5 years ago

@ebeshero and I were discussing how to create plain text files with XQuery. I'm trying to output a csv file with location information from the collection of Ulysses files. Here's my current XQuery expressions:

xquery version "3.1";
declare variable $ulysses as document-node()+ := collection('/db/ulysses/ulysses');
declare variable $ThisFileContent := 
{
let $lotusEaters := $ulysses[descendant:: chapter[@chapterName = "The Lotus Eaters"]]
let $location := $lotusEaters//location
let $locationName := $location//@name/string()
for $i in $locationName 
let $lat := $location[@name = $i]//@lat/string()
let $lon := $location[@name = $i]//@lon/string()
return {string-join(($i, $lat, $lon), ",")}
};

let $filename := "ulyssesLotusEatersLoc.csv"
let $doc-db-uri := xmldb:store("/db/frc23", $filename, $ThisFileContent)
return $doc-db-uri

This is basically the overarching structure that was used to output an html file from Pokemon XQuery expressions, except that the File Content variable is not being generated as an element node.

When I try to evaluate these expressions, it will not compile because it says that the curly braces are incorrectly placed. I looked at the information about generating a plain text file from here, but it didn't include much about this specific issue.

@ebeshero, why wouldn't it work now? Is there something else that I should change?

ebeshero commented 5 years ago

@frabbitry First, use my XQuery-to-Network-Analysis tutorial --and if you scroll down you'll see a giant code block showing an example of generating and saving a text file to the eXist-db: http://dh.newtfire.org/NetworkExercise1.html

I just realized that, of course, you simply don't need the curly braces at all, because you are not generating a document made from markup nodes!

frabbitry commented 5 years ago

@ebeshero Thanks for your help. I just got it to work with these expressions:

xquery version "3.1";
declare variable $ulysses as document-node()+ := collection('/db/ulysses/ulysses');
declare variable $ThisFileContent := 
string-join
(
let $lotusEaters := $ulysses[descendant:: chapter[@chapterName = "The Lotus Eaters"]]
let $location := $lotusEaters//location
let $locationName := $location//@name/string()=>distinct-values()
for $i in $locationName 
let $lat := $location[@name = $i]//@lat/string()
let $lon := $location[@name = $i]//@lon/string()
return string-join(($i, $lat, $lon), ",")
, "
");

let $filename := "ulyssesLotusEatersLoc.csv"

let $doc-db-uri := xmldb:store("/db/frc23", $filename, $ThisFileContent, "text/plain")
return $doc-db-uri

On to expanding this over the collection and mapping!