0

I am pulling a document from the database, selecting a part of the document, and transforming that part to json using json:transform-to-json, using an empty json:config("custom") for the configuration. The xml part that I am selecting does not have any attributes, only elements, yet in the transformation something is triggering the case where an element has both a value and an attribute, and is adding a _value element to the json which contains newline characters.

Here is a test program I ran where I pulled the content from the database, and returned the content, the transform of the content, and then a transform of hardcoded content which I had just copied from the console for the first part. My expectation is that the transformations would be the same, but they are not.

import module namespace json="http://marklogic.com/xdmp/json"
     at "/MarkLogic/json/json.xqy";

declare namespace pdbm = "http://schemas.abbvienet.com/people-db/model";

let $person := fn:collection()[1]//pdbm:person/pdbm:account

let $node :=
  <account xmlns:pdbe="http://schemas.abbvienet.com/people-db/envelope" xmlns="http://schemas.abbvienet.com/people-db/model">
    <domain>ABBVIENET</domain>
    <username>KOPECOX</username>
    <status>ENABLED</status>
  </account>

return ($person, json:transform-to-json($person, json:config("custom")), json:transform-to-json($node, json:config("custom")))

The selected content from the db, $person in this case transforms to:

{
  "account": {
    "domain": "ABBVIENET", 
    "username": "KOPECOX", 
    "status": "ENABLED", 
    "_value": "\n   "
  }
}

Whereas the $node, which is exactly the same content transforms to:

{
  "account": {
    "domain": "ABBVIENET", 
    "username": "KOPECOX", 
    "status": "ENABLED"
  }
}
TJ Tang
  • 921
  • 6
  • 17

2 Answers2

3

The custom json:config is in fact a map:map to which you can add extra flags, for instance the whitespace ignore. That seemed to help:

xquery version "1.0-ml";

import module namespace json="http://marklogic.com/xdmp/json"
     at "/MarkLogic/json/json.xqy";

let $node :=
  <account xmlns:pdbe="http://schemas.abbvienet.com/people-db/envelope" xmlns="http://schemas.abbvienet.com/people-db/model">
    <domain>ABBVIENET</domain>
    <username>KOPECOX</username>
    <status>ENABLED</status>
    { "&#10;" }
  </account>

let $config := json:config("custom")
let $_ := map:put($config, "whitespace", "ignore")
return json:transform-to-json($node, $config)

See also the documentation for json:config: http://docs.marklogic.com/json:config

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35
2

If your XML has text which you want preserved you can change the default json field name by setting the "text-value" key.

  • text_value The JSON member name to use for text when an element contains both attributes and text.

http://docs.marklogic.com/json:config

DALDEI
  • 3,722
  • 13
  • 9
  • Thanks. In this case I do not have text that I want to preserve, but I am still getting some garbage there. And this is only when pulling from a document in the database. The same XML in code works fine. – TJ Tang Jun 08 '16 at 18:04