【问题标题】:MarkLogic TDE XPath Values from nested JSON arrays来自嵌套 JSON 数组的 MarkLogic TDE XPath 值
【发布时间】:2021-06-20 13:13:30
【问题描述】:

虽然MarkLogic TDE Xpath values from JSON string array 有所帮助,但我仍然难以从嵌套的 JSON 数组中访问值。

这是一个带有嵌套数组的示例 JSON 文档:

  { 
    "instance": {
      "ID": 7,
      "groups": [
        {
          "type": "parent",
          "items": [
            {
              "type": "child",
              "items": [
                {
                  "payload": {
                    "name": "Frank Lee",
                    "age": 22
                  }
                },
                {
                  "payload": {
                    "name": "Sal Lee",
                    "age": 21
                  }
                },
                {
                  "payload": {
                    "name": "Ro Mance",
                    "age": 27
                  }
                }
              ]          
            }
          ]
        }
      ]
    }
  }

使用以下模板,我可以提供一个由实例 ID 和组数组类型名称组成的 SQL 视图:

xquery version "1.0-ml"; 

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

let $nested-json :=
<template xmlns="http://marklogic.com/xdmp/tde">
  <context>/instance/array-node()/groups</context>
  <enabled>true</enabled>
  <rows>
    <row>
      <schema-name>JSON_Example</schema-name>
      <view-name>nested_json</view-name>
      <columns>
        <column>
          <name>instance_ID</name>
          <scalar-type>int</scalar-type>
          <val>../../ID</val>
        </column>
        <column>
          <name>group_type</name>
          <scalar-type>string</scalar-type>
          <val>type/data()</val>
          <nullable>true</nullable>
          <invalid-values>ignore</invalid-values>
        </column>
      </columns>
    </row>
  </rows>
</template>

(:return tde:validate($nested-json):)
return tde:template-insert("/nested_json.xml", $nested-json)

但是,尝试在嵌套项数组中创建值的视图,例如有效负载名称,是非常具有挑战性的。此模板不起作用:

xquery version "1.0-ml"; 

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

let $nested-json :=
<template xmlns="http://marklogic.com/xdmp/tde">
  <context>/instance/array-node()/groups/array-node()/items/array-node()/item/payload</context>
  <enabled>true</enabled>
  <rows>
    <row>
      <schema-name>JSON_Example</schema-name>
      <view-name>nested_json_array</view-name>
      <columns>
        <column>
          <name>instance_ID</name>
          <scalar-type>int</scalar-type>
          <val>../../../../ID</val>
        </column>
        <column>
          <name>group_type</name>
          <scalar-type>string</scalar-type>
          <val>name</val>
          <nullable>true</nullable>
          <invalid-values>ignore</invalid-values>
        </column>
      </columns>
    </row>
  </rows>
</template>

(:return tde:validate($nested-json):)
return tde:template-insert("/nested_json_array.xml", $nested-json)

有什么想法吗?

【问题讨论】:

    标签: sql json marklogic tde


    【解决方案1】:

    问题是 XPath 在 MarkLogic 中处理 JSON 的方式可能会导致混淆。某些级别会被隐式跳过,而您不会注意到它。它有助于理解完整的 JSON 树在 XPath 术语中的样子。正确且最明确的上下文路径是:

    <context>/object-node()/object-node('instance')/array-node('groups')/object-node()/array-node('items')/object-node()/array-node('item')/object-node()/object-node('payload')</context>
    

    这表明你必须升到 7 级才能达到祖先 instance

    <val>../../../../../../../ID</val>
    

    这不会增加可读性,因此作为替代方案,您可以利用 ancestor 轴,并简化 XPath 表达式。所以使用它作为上下文:

    <context>/instance/groups/items/item/payload</context>
    

    这要联系ID

    <val>ancestor::instance/ID</val>
    

    HTH!

    【讨论】:

    • 我注意到 JSON 文档中最嵌套的数组需要更正才能正常工作。
    • 我看到你编辑了 JSON 示例,但它对我来说就像以前一样工作。在 items 数组中使用 items 数组,您将需要相应地调整上下文 XPath..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多