0
votes

I want to convert CSV to XML using XQuery on BaseX (currently using BaseX v9.4.1). With columns tab separated the CSV file Configuration.tsv (input) looks like this:

Datasets    Autosar             Fibex           TSS0001  TSS0002  TSS0003
DS0001      AutosarFoo01.arxml  FibexFoo01.xml  x        x  
DS0002      AutosarFoo02.arxml  FibexFoo02.xml  x                 x
DS0003      AutosarFoo03.arxml  FibexFoo03.xml  x                 x

The script has tssid as input parameter. Setting tssid := TSS0001 the expected output XML is:

<database name="DS0001" autosar="AutosarFoo01.arxml" fibex="FibexFoo01.arxml"/>
<database name="DS0002" autosar="AutosarFoo02.arxml" fibex="FibexFoo02.arxml"/>
<database name="DS0003" autosar="AutosarFoo03.arxml" fibex="FibexFoo03.arxml"/>

and with tssid := TSS0003:

<database name="DS0002" autosar="AutosarFoo02.arxml" fibex="FibexFoo02.arxml"/>
<database name="DS0003" autosar="AutosarFoo03.arxml" fibex="FibexFoo03.arxml"/>

The script works fine but the third attribute value (fibex) is missing, so the output looks differently like this:

<database name="DS0001" autosar="AutosarFoo01.arxml" fibex=""/>
<database name="DS0002" autosar="AutosarFoo02.arxml" fibex=""/>
<database name="DS0003" autosar="AutosarFoo03.arxml" fibex=""/>

or with tssid := TSS0003:

<database name="DS0002" autosar="AutosarFoo02.arxml" fibex=""/>
<database name="DS0003" autosar="AutosarFoo03.arxml" fibex=""/>

I cannot find something wrong with this code ?!? :

xquery version "3.1" encoding "utf-8";

declare namespace test="unittest";

declare namespace map="http://www.w3.org/2005/xpath-functions/map";

declare variable $test:tssid := 'TSS0003';

declare variable $test:testsuitesfile := '/Users/ms/Projekte/UnitTests/Configuration.tsv';

declare variable $test:options := map { 'header' : 'yes', 'format' : 'attributes', 'separator' : 'tab' };

declare variable $test:xml := csv:doc($test:testsuitesfile,$test:options);

(: get the positions :)

declare variable $test:positionDataId := 1 ;

declare variable $test:positionTssCheck := $test:xml/csv/record[position() = 1]/entry[text() = $test:tssid]/count(./preceding-sibling::entry) + 1 ;

declare variable $test:positionAutosar := $test:xml/csv/record[position() = 1]/entry[text() = 'Autosar']/count(./preceding-sibling::entry) + 1 ;

declare variable $test:positionFibex := $test:xml/csv/record[position() = 1]/entry[text() = 'Fibex']/count(./preceding-sibling::entry) + 1 ;

declare variable $test:dbsetbycsv as map(*) := map:merge(for $record in $test:xml/csv/record[entry[position() = $test:positionTssCheck and text()='x']] return map:entry($record/entry[position() = $test:positionDataId]/text(),<database name="{$record/entry[position() = $test:positionDataId]/text()}" autosar="{$record/entry[position() = $test:positionAutosar]/text()}" fibex="{$record/entry[position() = $test:positionFibex]/text()}"/>));

declare variable $test:dbset := $test:dbsetbycsv;

declare function test:dump() {
  for-each(
    map:keys($test:dbset),
    function($k) { 
      $test:dbset($k)
    }
  )
};

let $result := test:dump()

return($result)
2
When I try your code as-is, the variables $test:positionTssCheck, $test:positionAutosar and $test:positionFibex are all the empty sequence () because you specified 'header' : 'yes' in your CSV parsing options and the column headers are therefore in the <entry name="..." attributes and not in the text() nodes of the first line. When I change the three lines to $test:xml/csv/record[position() = 1]/entry[@name = '...']/... (replacing text() by @name), it all works as expected. Could you update the code so that it matches the output you describe? - Leo Wörteler
@LeoWörteler thank you for your response. Please find sample code and data here. ScriptOrig.xqy does not work as expected, but ScriptModified.xqy works fine. - user5924595

2 Answers

0
votes

It seems that

$xml/csv/record[entry[@name = $tssid and . = 'x']] ! <database name="{entry[@name = 'Datasets']}" autosar="{entry[@name = 'Autosar']}" fibex="{entry[@name = 'Fibex']}"/>

might suffice, given that the entry elements have name attributes.

0
votes

It's a bug. Fixed now with BaseX 9.4.3 beta.