Xquery 有没有一种方法可以在没有其他条件的情况下拥有多个if?
我需要测试一个节点的3个属性。 问题是,我必须为错误中的每个属性返回错误,我不知道如何以一种简单的方式实现这一点 xquery不是很灵活,所以。。。没有那么多尝试Xquery 有没有一种方法可以在没有其他条件的情况下拥有多个if?,xquery,Xquery,我需要测试一个节点的3个属性。 问题是,我必须为错误中的每个属性返回错误,我不知道如何以一种简单的方式实现这一点 xquery不是很灵活,所以。。。没有那么多尝试 for $theHoldingListsField in //DisplaySettingCol/theDisplaySetting/theHoldingListsFields return if ($theHoldingListsField/@AFL != "MANDATORY") then
for $theHoldingListsField in //DisplaySettingCol/theDisplaySetting/theHoldingListsFields
return
if ($theHoldingListsField/@AFL != "MANDATORY") then
(
<error id="DPR-CKSEM-DEP-SMF142-2">
<args>
<arg value="{$theHoldingListsField/ancestor::node()/@id}"/>
<arg value="AFL = {$theHoldingListsField/@AFL}"/>
</args>
<location value="{functx:path-to-node-with-pos($theHoldingListsField)}"/>
</error>
)
else if ($theHoldingListsField/@attitudeIndicator != "MANDATORY") then
(
<error id="DPR-CKSEM-DEP-SMF142-2">
<args>
<arg value="{$theHoldingListsField/ancestor::node()/@id}"/>
<arg value="attitudeIndicator = {$theHoldingListsField/@attitudeIndicator}"/>
</args>
<location value="{functx:path-to-node-with-pos($theHoldingListsField)}"/>
</error>
)
用于//display settingcol/thedisplay setting/theholdinglists字段中的$theholdinglists字段
返回
如果($theHoldingListsField/@AFL!=“必填”),则
(
)
否则,如果($theHoldingListsField/@attitudeIndicator!=“必填”),则
(
)
因此,在本例中,我希望能够一次触发3个错误,而不是其中一个(像现在这样)。
我甚至不知道这是否可能
谢谢 在标准XQuery中,没有没有
if
而没有else
,因为if
/那么/else
是一个表达式,在每种情况下都必须计算为某个返回值(请参阅)
如果希望在不满足错误条件时返回空序列,则可以显式地针对每个错误分别执行此操作。然后,可以将所有零元素或一元素序列聚集到一个元素序列中,该元素序列将自动展平:
for $theHoldingListsField in //DisplaySettingCol/theDisplaySetting/theHoldingListsFields
return (
if ($theHoldingListsField/@AFL != "MANDATORY") then (
<error id="DPR-CKSEM-DEP-SMF142-2">
<args>
<arg value="{$theHoldingListsField/ancestor::node()/@id}"/>
<arg value="AFL = {$theHoldingListsField/@AFL}"/>
</args>
<location value="{functx:path-to-node-with-pos($theHoldingListsField)}"/>
</error>
) else (),
if ($theHoldingListsField/@attitudeIndicator != "MANDATORY") then (
<error id="DPR-CKSEM-DEP-SMF142-2">
<args>
<arg value="{$theHoldingListsField/ancestor::node()/@id}"/>
<arg value="attitudeIndicator = {$theHoldingListsField/@attitudeIndicator}"/>
</args>
<location value="{functx:path-to-node-with-pos($theHoldingListsField)}"/>
</error>
) else ()
)
用于//display settingcol/thedisplay setting/theholdinglists字段中的$theholdinglists字段
返回(
如果($theHoldingListsField/@AFL!=“必填”),则(
)else(),
如果($theHoldingListsField/@attitudeIndicator!=“必填”),则(
)else()
)
另一个选择是采用功能性更强的编程方法
我们可以将您的测试概括为一个在HoldingListsField上运行的函数,因为它只有两个不变量,即属性名($attr name
)和错误代码($error id
)
我们基本上循环您要测试的属性(带有错误代码),并在每个属性上调用local:test
函数,例如
declare function local:test($theHoldingListsField, $attr-name, $error-id) {
$theHoldingListsField/@*[local-name() eq $attr-name][. ne "MANDATORY"] !
<error id="{$error-id}">
<args>
<arg value="{$theHoldingListsField/ancestor::node()/@id}"/>
<arg value="{$attr-name} = {.}"/>
</args>
<location value="{functx:path-to-node-with-pos($theHoldingListsField)}"/>
</error>
};
let $tests := (
["AFL", "DPR-CKSEM-DEP-SMF142-2"],
["attitudeIndicator", "DPR-CKSEM-DEP-SMF142-2"]
)
for $theHoldingListsField in //DisplaySettingCol/theDisplaySetting/theHoldingListsFields
let $test-fn := local:test($theHoldingListsField, ?, ?)
return
$tests ! fn:apply($test-fn, .)
declare function local:test($theHoldingListsField,$attr name,$error id){
$theHoldingListsField/@*[local-name()eq$attr name][.ne“必填”]!
};
让$tests:=(
[“AFL”、“DPR-CKSEM-DEP-SMF142-2”],
[“姿态指示器”、“DPR-CKSEM-DEP-SMF142-2”]
)
对于//display settingcol/thedisplay setting/theholdinglists字段中的$theholdinglists字段
let$test fn:=local:test($theHoldingListsField,,?)
返回
$tests!fn:应用($test fn,)
上面的示例使用了XQuery 3.1功能,如数组([]
)、部分函数应用(?
)、简单映射运算符(!
)和更高阶函数(fn:apply
)。我建议学习XQuery3.1W3C规范中的内容
这也可以被重写以删除
的,而是让本地:测试
功能在所有字段(即
保持列表字段
)上运行。首先将重复的代码放入一个函数:
declare function local:check($att as attribute()) as element(error)? {
if ($att != "MANDATORY") then (
<error id="DPR-CKSEM-DEP-SMF142-2">
<args>
<arg value="{$att/../ancestor::node()/@id}"/>
<arg value="{name($att)} = {$att}"/>
</args>
<location value="{functx:path-to-node-with-pos($att/..)}"/>
</error>
) else ()
};
我无法理解你想要实现什么。在标准XQuery中,
if
表达式始终具有else
分支,但您可以使用例如if(condition)then someResult()
使else
分支返回空序列。如果您想将该error
元素用作一种“模板”,那么编写一个以属性节点和/或其父元素为参数的函数可能会有所帮助,然后在函数内部您可以参数化条件,当然还有error
结果元素的创建。
for $theHoldingListsField in //DisplaySettingCol/theDisplaySetting/theHoldingListsFields
return (local:check($theHoldingListsField/@AFL),
local:check($theHoldingListsField/@attitudeIndicator),
...)