Dixit Singla
Dixit Singla

Reputation: 2620

marklogic xdmp:http-post options parameter issue

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.

Upvotes: 1

Views: 114

Answers (2)

grtjn
grtjn

Reputation: 20414

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!

Upvotes: 1

bosari
bosari

Reputation: 2000

That's because you are trying to fetch values from an xml without namespace and putting it in an xml with namespace. You can modify your code to this -

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

For more understanding on how namespace work go through https://docs.marklogic.com/guide/xquery/namespaces#chapter

Upvotes: 1

Related Questions