问题描述
我想在BaseX上使用XQuery(当前使用BaseX v9.4.1)将CSV转换为XML。 CSV文件Configuration.tsv
(输入)与“列”标签分开时,如下所示:
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
脚本以tssid
作为输入参数。设置tssid := TSS0001
XML 期望 输出是:
<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"/>
并带有tssid := TSS0003
:
<database name="DS0002" autosar="AutosarFoo02.arxml" fibex="FibexFoo02.arxml"/>
<database name="DS0003" autosar="AutosarFoo03.arxml" fibex="FibexFoo03.arxml"/>
该脚本可以正常工作,但是缺少第三个属性值(fibex
),因此输出看起来像这样:
<database name="DS0001" autosar="AutosarFoo01.arxml" fibex=""/>
<database name="DS0002" autosar="AutosarFoo02.arxml" fibex=""/>
<database name="DS0003" autosar="AutosarFoo03.arxml" fibex=""/>
或使用tssid := TSS0003
:
<database name="DS0002" autosar="AutosarFoo02.arxml" fibex=""/>
<database name="DS0003" autosar="AutosarFoo03.arxml" fibex=""/>
我找不到此代码有问题吗?!? :
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)
解决方法
这是一个错误。现在已通过BaseX 9.4.3 Beta修复。
,似乎
$xml/csv/record[entry[@name = $tssid and . = 'x']] ! <database name="{entry[@name = 'Datasets']}" autosar="{entry[@name = 'Autosar']}" fibex="{entry[@name = 'Fibex']}"/>
只要entry
元素具有name
属性,就足够了。