oozcitak / xmlbuilder-js

An XML builder for node.js
MIT License
918 stars 107 forks source link

separateArrayItems #249

Closed btsimonh closed 3 years ago

btsimonh commented 3 years ago

Hi,

I have another thorny one.

I want to produce an element

<p someattr="something"><span someattr2="something2">line1</span><br/><span someattr2="something2">line2</span></p>

Looking at the documentation, having the span elements separated by the br element is only possible using separateArrayItems.

However, I can't see how I could add someattr to p.....

(I know I should move on to v2, but... always worth asking).

Must I rework the code to create the xml manually using .ele() and .up() - or is there some simple trick I'm missing? Or could I phreak the writer (again) in some simple way?

br,

Simon

oozcitak commented 3 years ago

Something like this:

{
  p: {
    "@someattr": "something",
    "#text": [
        { span: { "@someattr2": "something2", "#text": "line1" } }
        { br: "" }
        { span: { "@someattr2": "something2", "#text": "line2" } }
    ]
}
btsimonh commented 3 years ago

thankyou :). I will play today, and if I get a chance, will work up some extra wiki text to describe...

Damn... I'm stuck down at 9.0.7 - feel like i'm in the way back time machine. It's obviously introduced after that - some work to upgrade (it's come via xml2js and node-red), or else I may just have to be creative (add a span marked as a BR and then replace in the text output would be enough :) ). Some of my customer are still on node 6.9.11 - an added complication as now you are >= node 8 :(.

oozcitak commented 3 years ago

Yes it was probably fixed in v11.

btsimonh commented 3 years ago

the quick and very dirty:

                        'span':[{
                                    "$": {
                                        "xml:id": "sp",
                                    },
                                    "_": "l1",
                                },
                            { 
                                "$": {
                                    "ISBR": "",
                                },
                                "_":'',
                            },
                            {
                                "$": {
                                    "xml:id": "sp2",
                                },
                                "_": "l2",
                            },
                        ],

.....

payload = payload.replace(/<span ISBR=""><\/span>/g, '<br/>');

p.s. just love the project, but still hate coffee..... :)