【问题标题】:BaseX XQuery issue with map merge?地图合并的 BaseX XQuery 问题?
【发布时间】:2020-09-01 09:21:20
【问题描述】:

我想在 BaseX 上使用 XQuery 将 CSV 转换为 XML(目前使用 BaseX v9.4.1)。使用列选项卡分隔 CSV 文件 Configuration.tsv (input) 如下所示:

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)

【问题讨论】:

  • 当我按原样尝试您的代码时,变量$test:positionTssCheck$test:positionAutosar$test:positionFibex 都是空序列(),因为您在CSV 解析选项中指定了'header' : 'yes',并且因此,列标题位于&lt;entry name="..." 属性中,而不是位于第一行的text() 节点中。当我将三行更改为$test:xml/csv/record[position() = 1]/entry[@name = '...']/...(将text() 替换为@name)时,一切正常。您能否更新代码以使其与您描述的输出相匹配?
  • @LeoWörteler 感谢您的回复。请找到示例代码和数据here。 ScriptOrig.xqy 不能按预期工作,但 ScriptModified.xqy 工作正常。

标签: xquery basex


【解决方案1】:

好像

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

可能就足够了,因为 entry 元素具有 name 属性。

【讨论】:

    【解决方案2】:

    这是一个错误。现已在 BaseX 9.4.3 beta 中修复。

    【讨论】:

    • 您在此处显示的代码无法编译,它至少需要在$fibex := [...] 之前添加一个let。但是,如果在这种情况下将属性值提取到let-bound 变量中确实改变了结果(修复了其他问题),那么您可能真的在 BaseX 中发现了一个错误。但是,我根本无法使用您提供的代码重现它。
    • @LeoWörteler 感谢您的回复。是的,let 不见了。所以here 你会找到示例代码和数据。 ScriptOrig.xqy 不能按预期工作,但 ScriptModified.xqy 工作正常。我猜想map:entry() 的值部分中的变量评估有问题。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多