简体   繁体   中英

Generate class from XSD

I have simple xml like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <xmi:XMI xmi:version="2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:mdmi="http://schema.omg.org/spec/MDMI/1.0">
      <xmi:Documentation 
xmi:exporter="Firestar MDMI Tool" xmi:exporterVersion="1.0">
      </xmi:Documentation>
    </xmi:XMI>

I have created xsd:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://schema.omg.org/spec/XMI/2.1">
    <xs:element name="XMI">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Documentation">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute type="xs:string" name="exporter"/>
                                <xs:attribute type="xs:string" name="xmi:exporterVersion"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:float" name="version"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

then generated java classes used JAXB. and use unmarshal.

Almost everything ok, but exporterVersion field is not pulled up from xml.

I tried use import to namespace. but nothing happens if set attributeFormDefault="qualified" that attribut exporter is not pulled up

In your schema change (notice the xmi: prefix as part of the name)

 <xs:attribute type="xs:string" name="xmi:exporterVersion"/>

to

<xs:attribute type="xs:string" name="exporterVersion"/>

You should not use the xmi: qualifier in the name for exporterVersion . That namespace is defined in the XML document, but is not defined in the schema.

More importantly, as far as I can tell, you don't need the XSD at all. It appears you are trying to use the schema for the XMI Mapping Specification directly and building an XML document that will comply with that schema. If that is the case, there is no need for your own schema.

At any rate, your XML document does not reference your own schema in any way, so it should have no effect on processing that XML document at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM