Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple elements with different values #159

Closed
localguru opened this issue May 24, 2017 · 10 comments
Closed

multiple elements with different values #159

localguru opened this issue May 24, 2017 · 10 comments
Labels

Comments

@localguru
Copy link

localguru commented May 24, 2017

Hi,

for a shibb IdP I'd like to generate something like this:

      <mdui:UIInfo>
        <mdui:DisplayName xml:lang="de">Mein Titel</mdui:DisplayName>
        <mdui:DisplayName xml:lang="en">My title</mdui:DisplayName>
        <mdui:Description xml:lang="de">Ein Test</mdui:Description>
        <mdui:Description xml:lang="en">a test</mdui:Description>
      </mdui:UIInfo>

My js code:

                                  'mdui:UIInfo': {
                                    'mdui:DisplayName': { '@xml:lang': 'de', '#text': 'Mein Titel' },
                                    'mdui:DisplayName': { '@xml:lang': 'en', '#text': 'My title' },
                                    'mdui:Description': { '@xml:lang': 'de', '#text': 'Ein Test' },
                                    'mdui:Description': { '@xml:lang': 'en', '#text': 'A test' },
                                  }

This code ends up in just one element of DisplayName and Description

      <mdui:UIInfo>
        <mdui:DisplayName xml:lang="en">My title</mdui:DisplayName>
        <mdui:Description xml:lang="en">a test</mdui:Description>
      </mdui:UIInfo>

Any ideas?

Ciao!

@oozcitak
Copy link
Owner

oozcitak commented May 25, 2017

JS objects cannot have duplicate keys. You need to wrap those elements in an array.

@localguru
Copy link
Author

localguru commented May 25, 2017

Hi,

could you give an code example please?

I tried this, but it's not working:

                                  'mdui:UIInfo': {
                                    "mdui:DisplayName": [
                                      {
                                        "@xml:lang": "de",
                                        "#text": "AAA"
                                      },
                                      {
                                        "@xml:lang": "en",
                                        "#text": "BBB"
                                      }
                                    ],

Output is

<mdui:DisplayName xml:lang="en">AAA  BBB</mdui:DisplayName>

Thanks

@localguru
Copy link
Author

localguru commented May 27, 2017 via email

@oozcitak
Copy link
Owner

Here is a workaround:

var obj = { 'mdui:UIInfo' : 
  [
    { 'mdui:DisplayName': { "@xml:lang": "de", "#text": "Mein Titel" } },
    { 'mdui:DisplayName': { "@xml:lang": "en", "#text": "My title" } },
    { 'mdui:Description': { "@xml:lang": "de", "#text": "Ein Test" } },
    { 'mdui:Description': { "@xml:lang": "en", "#text": "a test" } }
  ]
};

xmlbuilder.create(obj, {separateArrayItems: true});

@localguru
Copy link
Author

Ozgur! Thank you, that's working 🥇

@oozcitak
Copy link
Owner

oozcitak commented May 29, 2017

Your code should also work too:

'mdui:UIInfo': {
  "mdui:DisplayName": [
    { "@xml:lang": "de", "#text": "AAA" },
    { "@xml:lang": "en", "#text": "BBB" }
  ]
}

I'll take a look.

@oozcitak oozcitak reopened this May 29, 2017
@oozcitak oozcitak added the Bug label Aug 15, 2017
@rgilling
Copy link

rgilling commented Dec 27, 2018

I'm trying to generate a moderately complicated XML, but I've hit an issue similar to this one. The solution works at the first level, but on the second level it doesn't output correctly. Here is a sample JSON object that I'm converting. The attribute groups are expanded correctly, but the nested attribute are not. I tried using both forms with each array element prefixed with 'attribute' and not. I'm trying to build a list of 'attribute' elements directly under each 'attribute-group'

{
      "category": {
        "@category-id": "twe-root",
        "display-name": {
          "#text": "Root",
          "@xml:lang": "x-default"
        },
        "description": {
          "#text": "Master Catalogue for Treasury Wines",
          "@xml:lang": "x-default"
        },
        "online-flag": {
          "#text": true
        },
        "attribute-groups": [
          {
            "attribute-group": {
              "@group-id": "wine",
              "display-name": {
                "#text": "Wine Attributes",
                "@xml:lang": "x-default"
              },
              "attribute": [
                {
                  "@attribute-id": "wineContentChannels",
                  "@system": false
                },
                {
                  "@attribute-id": "wineTastingNotesPDF",
                  "@system": false
                },
                {
                  "@attribute-id": "wineCOGS",
                  "@system": false
                },
                {
                  "@attribute-id": "wineCollection",
                  "@system": false
                },
                {
                  "@attribute-id": "wineType",
                  "@system": false
                },
                {
                  "@attribute-id": "wineVariety",
                  "@system": false
                },
                {
                  "@attribute-id": "wineBottleType",
                  "@system": false
                },
                {
                  "@attribute-id": "wineVintage",
                  "@system": false
                }
              ]
            }
          },
          {
            "attribute-group": {
              "@group-id": "coreProduct",
              "display-name": {
                "#text": "Core Product Attributes",
                "@xml:lang": "x-default"
              },
              "attribute": [
                {
                  "@attribute-id": "csrOnly",
                  "@system": false
                }
              ]
            }
          }
        ]
      }
    },

oozcitak added a commit that referenced this issue Feb 7, 2019
@oozcitak
Copy link
Owner

oozcitak commented Feb 7, 2019

Testing with the current master branch at d8d2447, it appears both issues are solved.

@localguru with this JS object:

{
  'mdui:UIInfo': {
    "mdui:DisplayName": [
      { "@xml:lang": "de", "#text": "AAA" }
      { "@xml:lang": "en", "#text": "BBB" }
    ]
  }
}

the output is

<mdui:UIInfo>
  <mdui:DisplayName xml:lang="de">AAA</mdui:DisplayName>
  <mdui:DisplayName xml:lang="en">BBB</mdui:DisplayName>
</mdui:UIInfo>

@rgilling with your JS object above the output is:

<category category-id="twe-root">
  <display-name xml:lang="x-default">Root</display-name>
  <description xml:lang="x-default">Master Catalogue for Treasury Wines</description>
  <online-flag>true</online-flag>
  <attribute-groups>
    <attribute-group group-id="wine">
      <display-name xml:lang="x-default">Wine Attributes</display-name>
      <attribute attribute-id="wineContentChannels" system="false"/>
      <attribute attribute-id="wineTastingNotesPDF" system="false"/>
      <attribute attribute-id="wineCOGS" system="false"/>
      <attribute attribute-id="wineCollection" system="false"/>
      <attribute attribute-id="wineType" system="false"/>
      <attribute attribute-id="wineVariety" system="false"/>
      <attribute attribute-id="wineBottleType" system="false"/>
      <attribute attribute-id="wineVintage" system="false"/>
    </attribute-group>
  </attribute-groups>
  <attribute-groups>
    <attribute-group group-id="coreProduct">
      <display-name xml:lang="x-default">Core Product Attributes</display-name>
      <attribute attribute-id="csrOnly" system="false"/>
    </attribute-group>
  </attribute-groups>
</category>

Can you please check if those are the expected results?

@littlebacklash
Copy link

@oozcitak I know this issue is closed, but a new scenario came up. How could I generate an xml with this structure?

      <text>
        <paragraph>Symbolic text number 1</paragraph>
        <table>
          <thead>
            <tr>
              <th>Desc</th>
              <th>Value</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>desc 1</td>
              <td>value 1</td>
            </tr>
          </tbody>
        </table>
        <paragraph>** Symbolic text number 2 **</paragraph>
      </text>

where there are 2 paragraphs nodes, but in differents parts of the xml body.
Thanks in advance!!

oozcitak added a commit that referenced this issue Aug 16, 2021
@oozcitak
Copy link
Owner

@littlebacklash You can do something like this (note the #text decorator):

const obj = {
  'text': {
    '#text': [
      { 'paragraph': 'Symbolic text number 1' }
      { 'table': '' }
      { 'paragraph': '** Symbolic text number 2 **' }
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants