json - 将同名的 XML 元素转换为同名的 JSON 属性

标签 json xquery marklogic marklogic-8

同名的 XML 元素和 JSON 属性可能是同级的。即:

<container>
  <value>value1</value>
  <value>value2</value>
</container>

object-node {
  "value" : "value1",
  "value" : "value2"
}

都是有效的,但我还没有找到一种有效的方法将一个转换为另一个。在 object-node 构造函数中动态构建属性是无效的,即:

object-node {
  for $v in $values
  return 'value' : $v
}

使用映射不起作用,因为重复的键名已折叠:

xdmp:to-json(map:new((
  map:entry("value", "value1"), 
  map:entry("value", "value2")))
  )

=> {"value":"value2"}

而在使用json:object时,最后一个key值是重复的:

json:object(<json:object>
  <json:entry key="value">
    <json:value>value1</json:value>
  </json:entry>
  <json:entry key="value">
    <json:value>value2</json:value>
  </json:entry>
</json:object>)

=> {"value":"value2", "value":"value2"}

使用 + 操作符连接 map 更好,但它会将重复的键合并成一个带有值数组的键({"value":["value1", "value2 "]}),这仍然不是我想要的。有没有办法在 XQuery 中动态构建同名的同级 JSON 属性?

最佳答案

您的 JSON 示例:

object-node {
  "value" : "value1",
  "value" : "value2"
}

不是真的有效:或者无论如何,最好避免。 RFC 7159 说:

When the names within an object are not unique, the behavior of software that receives such an object is unpredictable. Many implementations report the last name/value pair only. Other implementations report an error or fail to parse the object, and some implementations report all of the name/value pairs, including duplicates.

关于json - 将同名的 XML 元素转换为同名的 JSON 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47602557/

相关文章:

php - 将编码字符转换回原始字符 - PHP

json - 在 JSON 模型中使用枚举类型访问结构体属性的语法

xml - 子结构上的 XPath 求和

xpath - 使用 xpath 检查字符串是否只有 ASCII 字符

marklogic - 如何查询特定 uri 上的 MarkLogic?

xquery - 有人可以解释一下外部变量在 XQuery 中是如何工作的吗?

python - 需要 : simple way to force json to decode to "normal" iterable python lists

javascript - 提高使用 jQuery 搜索 JSON 对象的性能

xml - 通过xquery更改元素的值

xquery - 在 Marklogic 中自定义排序