Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.84k stars 596 forks source link

how to serialize xml when xsd contain choice #653

Closed meeting-you closed 1 year ago

meeting-you commented 1 year ago

for example: person.xsd

<?xml version = "1.0"?>
<xs:schema targetNamespace="http://iso.org/person/1.0.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:me="http://iso.org/person/1.0.0" elementFormDefault="qualified">
    <xs:element name="person" type="me:MyPerson"/>

    <xs:complexType name="MyPerson" xml:space="default">
        <xs:choice minOccurs="1" maxOccurs="unbounded">
            <xs:element name='man' type="me:Person" minOccurs='1'
                        maxOccurs='1'/>
            <xs:element name='woman' type="me:Person" minOccurs='1'
                        maxOccurs='1'/>
        </xs:choice>
    </xs:complexType>

    <xs:complexType name="Person">
        <xs:attribute name='name' type='xs:string' use="required"/>
        <xs:attribute name='age' type='xs:string' use="required"/>
    </xs:complexType>
</xs:schema>

person.xml

<?xml version = "1.0"?>
<person xmlns="http://iso.org/person/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://iso.org/my/1.0.0 person.xsd">
    <man name="a" age="1"/>
    <woman name="b" age="2"/>
    <man name="c" age="3"/>
</person>

how to serialize like example xml and parse xml