尝试从另一个config构建选项参数,以传递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"命名空间时,获得正确的输出。
发布于 2020-02-20 08:57:24
对,是这样。在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哈哈!
发布于 2020-02-20 08:54:14
这是因为您试图从没有命名空间的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的更多了解
https://stackoverflow.com/questions/60313675
复制相似问题