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

tag with attributes not working #52

Closed
HarriAlasi opened this issue Oct 1, 2021 · 5 comments
Closed

tag with attributes not working #52

HarriAlasi opened this issue Oct 1, 2021 · 5 comments

Comments

@HarriAlasi
Copy link

HarriAlasi commented Oct 1, 2021

I am trying to generate

<Amt Ccy="EUR">158.4</Amt> (as part of a larger SEPA payment xml)

Following the examples 4 and 7, I came up with
this.creditTransferTxXml.CdtTrfTxInf.Amt = {_attrs: {Ccy:SEPAInfo.payment.currency}, _content: SEPAInfo.payment.amount.toString()};

However, this produces
<Amt Ccy="EUR"><_content>158.4</_content><Amt>

I know the example #4 had only one tag, so I mixed it with example #7 and added a string instead of an object to the _content object.

I tried to do it with two steps(first add attribute, then add content), but couldn't figure out that either

Tried it with name attrtibute as well:
this.creditTransferTxXml.CdtTrfTxInf.Amt = {_name: 'Amt', _attrs: {Ccy:SEPAInfo.payment.currency}, _content: SEPAInfo.payment.amount.toString()};

However, this produced what I want, and then an extra tag wrapped around it
<Amt><Amt Ccy="EUR">158.4</Amt></Amt>

Syntactically it tries to add and Amt element to existing Amt element. I'm just trying to modfy the existing one

@HarriAlasi HarriAlasi changed the title nested tag with attributes not working tag with attributes not working Oct 1, 2021
@davidcalhoun
Copy link
Owner

davidcalhoun commented Oct 1, 2021

I tried this reduced test case and it seemed to work as expected:

toXML({
  _name: 'Amt',
  _attrs: { Ccy: 'EUR' },
  _content: 158.4
});
// -> <Amt Ccy="EUR">158.4</Amt>

Maybe something else is going on? What happens if you console.log() SEPAInfo.payment.currency and SEPAInfo.payment.amount.toString()? Is the content what you expect it to be?

@HarriAlasi
Copy link
Author

HarriAlasi commented Oct 1, 2021

Hi, thanks for the reply. Just before you posted it, I left for the day, so I couodnt verify, but let me give blind programming a go

I think where the misunderstanding comes from is that you did the test case in a reduced fashion. What if you wrapped the Amt tag inside another element, such as

{Element1:
  Amt: {
    _name: 'Amt',
    _attrs: {
      Ccy: 'EUR'
    },
    _content: 158.4
  }
  ChildElement: "ChildValue" 
} 

What I want to happen
<Element><Amt Ccy="EUR">158.4</Amt><ChildElement>ChildValue</ChildElement></Element>

@davidcalhoun
Copy link
Owner

davidcalhoun commented Oct 1, 2021

A good way to think of this is that Element has multiple children, so it makes sense to think of the value of Element as an array:

toXML({
  Element: [
    {
      _name: 'Amt',
      _attrs: { Ccy:'EUR' },
      _content: "158.4"
    },
    {
      ChildElement: 'ChildValue'
    }
  ]
});
// -> <Element><Amt Ccy="EUR">158.4</Amt><ChildElement>ChildValue</ChildElement></Element>

This also works if you prefer consistency:

toXML({
  Element: [
    {
      _name: 'Amt',
      _attrs: { Ccy:'EUR' },
      _content: "158.4"
    },
    {
      _name: 'ChildElement',
      _content: "ChildValue"
    }
  ]
});
// -> <Element><Amt Ccy="EUR">158.4</Amt><ChildElement>ChildValue</ChildElement></Element>

@HarriAlasi
Copy link
Author

HarriAlasi commented Oct 1, 2021

Indeed It is. Might I suggest updating the examples (maybe number 2, because array syntax is not needed only for duplicate keys, but also for multiple child nodes, which can very well be distinct.

I have re-read the examples and the only one this idea comes out on is example number 9 & 10 (and maybe 16 as well, although not mentioned directly)

@HarriAlasi
Copy link
Author

Either way, thank you and closing this out

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

No branches or pull requests

2 participants