Warm tip: This article is reproduced from stackoverflow.com, please click
marklogic

marklogic xdmp:http-post options parameter issue

发布于 2020-05-05 10:59:16

Trying to build options parameter from another config XML to pass in the xdmp:http-post function.

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

output of the above code is:

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

No able to understand why the xpath is returning blank. On removing the xmlns="xdmp:http" namespace getting the correct output.

Questioner
Dixit Singla
Viewed
64
grtjn 2020-02-20 16:57

Correct. It is a very subtle side-effect of using literal elements in default namespace inside your XQuery. Simplest is to use *: prefix wildcarding:

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

You can also pre-calc the values before the literal elements:

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

Or use element constuctors:

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!