madec-project / ezvis

A dashboard to visualize a synthesis on a structured corpus, using several charts (pies, histograms, ...)
https://ezvis.readthedocs.org/
17 stars 5 forks source link

How to create data.json fields using xml elements depending on its content ? #98

Closed gmella closed 6 years ago

gmella commented 6 years ago

How can man injest $e1 and $e2 in the database according following xml shape ?

<records>
  <record>
    <element> <name>e1</name> <value>va1</value> </element>
    <element> <name>e2</name> <value>va2</value> </element>
  </record>
  <record>
    <element> <name>e2</name> <value>vb2</value> </element>
    <element> <name>e1</name> <value>vb1</value> </element>
  </record>
</records>

I did not manage to find the proper jbj expression.

Regards, Guillaume

parmentf commented 6 years ago

Here is what I did (but, it treats only first record):

input:

"<records><record><element><name>e1</name><value>va1</value></element><element><name>e2</name><value>va2</value></element></record><record><element><name>e2</name><value>vb2</value></element><element><name>e1</name><value>vb1</value></element></record></records>"

jbj stylesheet:

{
  "$elements": {
    "parseXML" : {
      "specialChar": "#",
      "longTag" : true
    },
    "select": ".element"
  },
  "$res": {
    "get": "elements",
    "foreach": {
      "get": "0",
      "$key": {
        "select": ".name .#text",
        "get": "0"
      },
      "$val": {
        "select": ".value .#text",
        "get": "0"
      },
      "mask": "key,val"
    },
    "array2object": ["key", "val"]
  },
  "mask": "res"
}

output:

{
  "res": {
    "e1": "va1",
    "e2": "vb2"
  }
}

Maybe you can start from there to yield all records (don't forget JBJ playground)?

parmentf commented 6 years ago

Maybe this stylesheeet is clearer (at least, it's shorter):

{
  "parseXML" : {
    "specialChar": "#",
    "longTag" : true
  },
  "select": ".element",
  "foreach": {
    "get": "0",
    "$key": {
      "select": ".name .#text",
      "get": "0"
    },
    "$val": {
      "select": ".value .#text",
      "get": "0"
    },
    "mask": "key,val"
  },
  "array2object": ["key", "val"]
}
gmella commented 6 years ago

Yes I tried the nice JBJ ! But my knowledge is not json/js oriented. I hope that lodex get quite the same conventions ;) Thank you for your help.

parmentf commented 6 years ago

Oh, here is a better solution:

{
  "parseXML" : {
    "specialChar": "#",
    "longTag" : true
  },
  "foreach": {
    "select": ".element",
    "foreach": {
      "get": "0",
      "$key": {
        "select": ".name .#text",
        "get": "0"
      },
      "$val": {
        "select": ".value .#text",
        "get": "0"
      },
      "mask": "key,val"
    },
    " array2object": ["key", "val"]
  }
}

gives:

{
  "records": [
    {
      "key": "e1",
      "val": "va1"
    },
    {
      "key": "e2",
      "val": "vb2"
    }
  ]
}