marklogic xdmp:http-post-options参数问题



尝试从另一个配置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"命名空间时获得正确的输出。

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

啊!

这是因为您试图从没有命名空间的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

最新更新