尝试从另一个配置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
HTH!