【问题标题】:XQuery switch testing nodeXQuery 切换测试节点
【发布时间】:2018-10-23 13:06:46
【问题描述】:

在 XQuery 3.1 中,此(测试)查询通过检查 name():来检查某些节点的存在:

declare variable $doc := 
  doc("/db/apps/deheresi/data/ms609_0013.xml"));

let $ele := $doc//tei:sic | $doc//tei:surplus

for $n in $ele

let $output := switch ($n/name())
            case ("sic")
                return ($n)
            case ("surplus")
                return ($n)
            default return ""
return $output

正确返回以下 XML:

<surplus reason="surplus">die</surplus>
<surplus reason="repeated">et Raimundum de las de Recaut</surplus>

现在,当我想对同一个文档运行我的实际查询以测试节点以生成 HTML 时,它没有找到相同的tei:surplus

declare variable $doc := 
  doc("/db/apps/deheresi/data/ms609_0013.xml"));

let $ele := $doc//tei:sic | $doc//tei:surplus

for $n in $ele 

let $output := switch ($n)
            case ($n/self::tei:sic)
                return (<span class="inter">
                        <i>ms. </i>
                        {$n/tei:orig/text()}
                        </span>,
                        <span class="diplo">
                        <i>corr. </i>
                        {$n/tei:corr/text()}
                        </span>)
            case ($n/self::tei:surplus[@reason="surplus"])
                return (<span><i>supp.</i>{$n/text()}</span>)
            case ($n/self::tei:surplus[@reason="repeated"])
                return (<span><i>supp. (dup.)</i>{$n/text()}</span>)
            default return  ""
 return $output

我在case 上测试节点的方式是否有问题,它在同一个文档中找不到tei:surplus

注意:当我对包含第一个案例 (tei:sic) 的文档执行相同操作时,它输出正常。显然,原则上测试应该有效!

提前致谢。

【问题讨论】:

  • XQuery switch 原子化了操作数表达式。在这种情况下,节点被简化为一个名称。因此需要在每个case 中测试某种字符串。由于这个原因,第二个开关将无法返回结果,因为我正在测试节点,因此原子化结果不可预测。
  • 请参阅w3.org/TR/xquery-31/#id-typeswitch,了解如何在 XQuery 3.1 中检查节点。

标签: xquery xquery-3.0


【解决方案1】:

switch 构造比较原子值。你可以这样使用它:

switch (node-name($n))
case QName("http://tei-namespace/", "sic") return <something/>

注意使用node-name() 而不是name() 以避免对命名空间前缀的任何依赖。

但使用 typeswitch 可能会更好:

typeswitch ($n)
case element(tei:sic) return <something/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多