首页 > 解决方案 > MarkLogic - Xpath on JSON document

问题描述

MarkLogic Version: 9.0-6.2

I am trying to apply Xpath in extract-document-data (using Query Options) on a JSON document shown below. I need to filter out "Channel" property if the underneath property "OptIn" has a value of "True".

    {
        "Category":
          {
            "Name": "Severe Weather",
            "Channels":[
              {
                "Channel":
                  {
                    "Name":"Email",
                    "OptIn": "True"
                  }
              },
              {
                "Channel":
                  {
                    "Name":"Text",
                    "OptIn": "False"
                  }
              }
             ]
          }
      }

I tried below code,

'<extract-document-data selected="include">'  +
'<extract-path>//*[OptIn="True"]/../..</extract-path>' +
'</extract-document-data>' +

which is only pulling from "Channel" property as shown below.

  [
    {
      "Channel": {
      "Name": "Email",
      "OptIn": "True"
      }
    }
  ]

But my need is to pull from parent "Category" property, but filter out the Channels that have OptIn value as False.

Any pointers?

标签: marklogic-9marklogic-dhf

解决方案


If I understand correctly, you'd like to extract 'Category', but only with those 'Channel's that have 'OptIn' equalling 'true', right?

Extract-document-data is not advanced enough for that. You best extract entire Categories which have at least one OptIn equalling true (//Category[//OptIn = 'true']), and use a REST transform on the search response to trim down the unwanted Channels..

HTH!


推荐阅读