温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - marklogic xdmp:http-post options parameter issue
marklogic

其他 - marklogic xdmp:http发布选项参数问题

发布于 2020-05-08 18:10:36

尝试从另一个配置XML构建options参数以传递xdmp:http-post函数。

let $db-config :=
  <config>
      <user-name>admin</user-name>
      <password>admin</password>
  </config>
let $options :=
        <options xmlns="xdmp:http">
            <authentication method="digest">
                <username>{$db-config/user-name/text()}</username>
                <password>{$db-config/password/text()}</password>
            </authentication>
        </options>
return $options

上面代码的输出是:

<options xmlns="xdmp:http">
  <authentication method="digest">
    <username>
    </username>
    <password>
    </password>
 </authentication>
</options>

无法理解为什么xpath返回空白。在删除xmlns="xdmp:http"名称空间时获取正确的输出。

查看更多

提问者
Dixit Singla
被浏览
80
grtjn 2020-02-20 16:57

正确。在XQuery的默认名称空间中使用文字元素的副作用非常微妙。最简单的方法是使用*:前缀通配符:

let $db-config :=
  <config>
      <user-name>admin</user-name>
      <password>admin</password>
  </config>
let $options :=
        <options xmlns="xdmp:http">
            <authentication method="digest">
                <username>{$db-config/*:user-name/text()}</username>
                <password>{$db-config/*:password/text()}</password>
            </authentication>
        </options>
return $options

您还可以在文字元素之前预先计算值:

let $db-config :=
  <config>
      <user-name>admin</user-name>
      <password>admin</password>
  </config>
let $user as xs:string := $db-config/user-name
let $pass as xs:string := $db-config/password
let $options :=
        <options xmlns="xdmp:http">
            <authentication method="digest">
                <username>{$user}</username>
                <password>{$pass}</password>
            </authentication>
        </options>
return $options

或使用元素构造器:

let $db-config :=
  <config>
      <user-name>admin</user-name>
      <password>admin</password>
  </config>
let $options :=
        element {fn:QName("xdmp:http", "options")} {
            element {fn:QName("xdmp:http", "authentication")} {
                attribute method { "digest" },

                element {fn:QName("xdmp:http", "username")} {
                    $db-config/user-name/text()
                },
                element {fn:QName("xdmp:http", "password")} {
                    $db-config/password/text()
                }
            }
        }
return $options

HTH!