marklogic xdmp:http-post 选项参数问题

marklogic xdmp:http-post options parameter issue

正在尝试从另一个配置构建选项参数 XML 以传入 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" 命名空间时得到正确的输出。

那是因为您试图从不带命名空间的 xml 中获取值并将其放入带命名空间的 xml 中。您可以将代码修改为 -

xquery version "1.0-ml";
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

通过 https://docs.marklogic.com/guide/xquery/namespaces#chapter

进一步了解命名空间的工作原理

正确。在 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!