xml-typescript icon indicating copy to clipboard operation
xml-typescript copied to clipboard

Creating mixed content

Open sshivananda opened this issue 6 years ago • 5 comments

I am trying to model classes that can support mixed content. From the example at https://www.npmjs.com/package/js2xmlparser I could do this with

var js2xmlparser = require("js2xmlparser");
 
var obj = {
    "phone": [
        {
            "@": {
                "type": "home"
            },
            "#": "123-555-4567"
        },
        {
            "@": {
                "type": "cell"
            },
            "#": "890-555-1234"
        }
    ],
    "email": "[email protected]"
};
 
console.log(js2xmlparser.parse("person", obj));

which gives me an xml

<?xml version='1.0'?>
<person>
    <phone type='home'>123-555-4567</phone>
    <phone type='cell'>890-555-1234</phone>
    <email>[email protected]</email>
</person>

How would I go about doing this using xml-decorators?

sshivananda avatar Jul 18 '18 08:07 sshivananda

@sshivananda Unfortunately this is not possible 🤔There would be something like a @Value annotation needed to mark a property as the value, of course. Basically what # does for js2xmlparser.

RobinBuschmann avatar Jul 21 '18 08:07 RobinBuschmann

I am trying to add support for this and would to happy to raise a PR if I succeed - do you have pointers on where I could start

sshivananda avatar Jul 21 '18 14:07 sshivananda

Hey @sshivananda, any help is appreciated 👍 So, here we go: The schema is processed in the XMLElement model. For instance the attributes (which are defined via @) are set here: https://github.com/RobinBuschmann/xml-typescript/blob/master/lib/models/XMLElement.ts#L163 Hope this helps!

RobinBuschmann avatar Jul 21 '18 15:07 RobinBuschmann

Robin, I created a modified version which offers a @XMLText decorator (with a single option required). It creates the missing # key inside the schema consumed by js2xmlparser . My local tests were positive. Are you interested in a PR?

rzacherl avatar Mar 20 '19 22:03 rzacherl

@rzacherl Yes, for sure 👍 Just create the PR so that I can review it

RobinBuschmann avatar Mar 21 '19 06:03 RobinBuschmann