I want following xml output and have following xsd
<msgBody>
<Contato>
<cd_id>11</cd_id>
<property key="name" value="Adde" />
<property key="Phone" value="34343" />
<property key="Address" value="address" />
</Contato>
<Contato>
<cd_id>12</cd_id>
<property key="name" value="BJ" />
<property key="Phone" value="7899" />
<property key="Address" value="sdfkjsdfsdf" />
</Contato>
</msgBody>
I have following xsd where i don't know how can i allow to have multiple property tags
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element name="Contato" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element type="property_data_type" name="property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="property_data_type">
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
Try this
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Contato"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="contao">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element maxOccurs="unbounded" ref="propertytype"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="propertytype">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Please add maxOccurs.
<xs:element type="property_data_type" name="property" maxOccurs="unbounded"/>
Alternatively, you can put any concrete limit like:
<xs:element type="property_data_type" name="property" maxOccurs="10"/>
It means you can only have a maximum of 10 property elements. The <maxOccurs> indicator specifies the maximum number of times an element can occur.
Related
I am new to XSDs format. I am trying to convert one xsd to java bean using xjc.But on converting I am getting multiple beans rather than getting just one bean.How to obtain one single bean from the XSD?
Below is my xsd.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TradeMessage" type="TradeMessageType"/>
<xs:simpleType name="empty-string">
<xs:restriction base="xs:string">
<xs:enumeration value="" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="decimal-or-empty">
<xs:union memberTypes="xs:decimal empty-string" />
</xs:simpleType>
<xs:complexType name="contractingEntityType">
<xs:sequence>
<xs:element type="tradingAccountType" name="tradingAccount"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="executingEntityType">
<xs:sequence>
<xs:element type="tradingAccountType" name="tradingAccount"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradeFeesType">
<xs:choice maxOccurs="unbounded" minOccurs="0">
<xs:element type="tradeFees_itemType" name="tradeFees_item"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="settlementCurrencyType">
<xs:sequence>
<xs:element type="xs:string" name="iSOCode"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradingAccountType">
<xs:sequence>
<xs:element type="xs:string" name="uniqueID"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="currencyType">
<xs:sequence>
<xs:element type="xs:string" name="iSOCode"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradingInstrumentType">
<xs:sequence>
<xs:element type="xs:string" name="CUSIP"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradeFees_itemType">
<xs:sequence>
<xs:element type="chargeCodeType" name="chargeCode"/>
<xs:element type="xs:string" name="inConsideration"/>
<xs:element type="currencyType" name="currency"/>
<xs:element type="xs:decimal" name="currencyValue"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="IncomingTradeMessageType">
<xs:sequence>
<xs:element type="IncomingTradeType" name="IncomingTrade"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TradeMessageType">
<xs:sequence>
<xs:element type="IncomingTradeMessageType" name="IncomingTradeMessage"/>
</xs:sequence>
<xs:attribute type="xs:string" name="schemaVersion"/>
</xs:complexType>
<xs:complexType name="chargeCodeType">
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="theirTradingAccountType">
<xs:sequence>
<xs:element type="xs:string" name="uniqueID"/>
<xs:element type="xs:string" name="organisationUniqueID"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tradeCurrencyType">
<xs:sequence>
<xs:element type="xs:string" name="iSOCode"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="transactionTypeType">
<xs:sequence>
<xs:element type="xs:string" name="uniqueID"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="channelType">
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="IncomingTradeType">
<xs:sequence>
<xs:element type="xs:string" name="blockTradeReference"/>
<xs:element type="xs:string" name="sourceReference"/>
<xs:element type="xs:string" name="sourceVersion"/>
<xs:element type="xs:decimal" name="nominal"/>
<xs:element type="xs:decimal" name="price"/>
<xs:element type="xs:string" name="tradeDate"/>
<xs:element type="xs:string" name="valueDate"/>
<xs:element type="transactionTypeType" name="transactionType"/>
<xs:element type="tradingInstrumentType" name="tradingInstrument"/>
<xs:element type="tradeCurrencyType" name="tradeCurrency"/>
<xs:element type="xs:string" name="cancelled"/>
<xs:element type="xs:string" name="tradeTime"/>
<xs:element type="tradeFeesType" name="tradeFees"/>
<xs:element type="xs:decimal" name="grossConsideration"/>
<xs:element type="xs:decimal" name="netConsideration"/>
<xs:element type="xs:decimal" name="settlementConsideration"/>
<xs:element type="settlementCurrencyType" name="settlementCurrency"/>
<xs:element type="decimal-or-empty" name="settlementExRate"/>
<xs:element type="xs:string" name="settlementExRateDirection"/>
<xs:element type="theirTradingAccountType" name="theirTradingAccount"/>
<xs:element type="executingEntityType" name="executingEntity"/>
<xs:element type="contractingEntityType" name="contractingEntity"/>
<xs:element type="decimal-or-empty" name="calculatedNetPrice"/>
<xs:element type="xs:string" name="dealingCapacity"/>
<xs:element type="xs:string" name="agreedPriceIndicator"/>
<xs:element type="xs:string" name="orderReference"/>
<xs:element type="xs:string" name="lastAmendedOnBusinessDate"/>
<xs:element type="xs:string" name="creationAsatDate"/>
<xs:element type="xs:string" name="bpsBranchAccountNumber"/>
<xs:element type="xs:string" name="bpsAccountType"/>
<xs:element type="xs:string" name="shortSell"/>
<xs:element type="decimal-or-empty" name="facilitationPercentage"/>
<xs:element type="xs:string" name="isStepOut"/>
<xs:element type="xs:string" name="stepOutBrokerMPID"/>
<xs:element type="xs:string" name="bargainConditionFO"/>
<xs:element type="channelType" name="channel"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
This XSD is giving me different beans for different properties but actually I would need just one bean comprising of all the properties. As per my requirement I would receive input message (as per above xsd) and convert it directly to java bean and use this bean for further processing. If there is some other better strategy ,kindly let me know.
This is how JAXB works. A complex schema type is mapped to a Java class, and since this schema has many complex types you get several Java classes. When unmarshalling an XML document, you'll get one TradeMessageType instance.
I have XML specified the following:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItem" type="tns:newsList"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="newsList">
<xs:list itemType="tns:news"/>
</xs:simpleType>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
I would like to have a list of the news in my response. However when I would like to create Java object with jaxb2 the xml returns the folllowing error when I run mvn clean compile -X:
org.xml.sax.SAXParseException: cos-st-restricts.1.1: The type 'newsList' is atomic, so its {base type definition}, 'tns:news', must be an atomic simple type definition or a built-in primitive datatype.
How I should change my XML to be able to compile?
In addition to using the built-in list types, you can create new list types by derivation from existing atomic types. You cannot create list types from existing list types, nor from complex types.
https://www.w3.org/TR/xmlschema-0/#ListDt
This is from one of my working XSD, a user with multiple addresses:
<xs:complexType name="user">
<xs:sequence>
<xs:element name="addresses" type="tns:addressData" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
Note that addressData is a complexType.
I guess this is what you need:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItems" type="tns:news" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://pro.com/balcao/xdto"
xmlns:tns="http://pro.com/balcao/xdto"
elementFormDefault="qualified">
<xs:element name="salvarCliente" >
<xs:complexType>
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="listarClientes">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="clientes" type="tns:cliente" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="cliente">
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I have the following xsd
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="controls" type="Controls"></xs:element>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Controls">
<xs:sequence>
<xs:element name="control" type="Control" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
How can I generate the following xml document
<?xml version = "1.0"?>
<screen ref="10_20_25_vwopstellen">
<name>VWOpstellen</name>
<controls>
<button>
<name>btnA</name>
<id>btn_10</id>
<action>click</action>
</button>
<textfield>
<name>fldA</name>
<id>fld_20</id>
</textfield>
</controls>
</screen>
Now it is generating the following xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<screen ref="scrVWOpstellen">
<name>VWOpstellen</name>
<controls>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnA</name>
</control>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnB</name>
</control>
</controls>
</screen>
And when i read my xml document I want a list of contols containing either buttons or textfields
List --> having Button and TextField
I have no idea, i think I should use substitution group but how. I tried something but the xsd is not generating any java code.
thanks
Johan
I'd suggest using an xs:choice, and also defining a target namespace, (http://my.target.namespace in my example), which can make working with JAXB easier in general.
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Another thought, sometimes the type derivation isn't really worth it. For something like this you might also just do:
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Button">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TextField">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
I have a XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="Structure">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="Tag1"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
<xs:element minOccurs="0" ref="Properties"/>
<xs:element minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="url"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag1">
<xs:complexType>
<xs:attribute name="attr"/>
<xs:attribute name="attr2"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag2">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element minOccurs="0" ref="Object"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
</xs:sequence>
<xs:attribute name="filter"/>
</xs:complexType>
</xs:element>
<xs:element name="Object">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="tag35">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="attr4"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attr48"/>
</xs:complexType>
</xs:element>
<xs:element name="element52">
<xs:annotation>
<xs:documentation>Text54</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="el58"/>
</xs:sequence>
<xs:attribute name="vector"/>
</xs:complexType>
</xs:element>
<xs:element name="el66" type="el66Type"/>
<xs:element name="el58">
<xs:complexType mixed="true">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag3">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" name="MetaProperty">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="value"/>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
<xs:attribute name="ID"/>
<xs:attribute name="language" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Property">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any minOccurs="0" processContents="skip"/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="type"/>
</xs:complexType>
</xs:element>
<xs:element name="Properties">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType mixed="true" name="el66Type">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:schema>
And an xml:
<Structure url="/Test/url">
<Tag1/>
<Tag2>
<Object>
<tag35/>
</Object>
</Tag2>
<Properties>
<Property name="bla" type="value1"/>
<Property name="bla2" type="value2"/>
</Properties>
</Structure>
I am doing a validation, and I am getting the following error:
org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'Tag2'. One of '{Tag1, Tag2, Tag3}' is expected.
I checked in previous questions and always the solution is related with elementFormDefault="qualified" but now it is not the case so, could it be possible that there is a bug there? Any clue, will be grateful.
Your XML is valid against your XSD as posted in your question.
The error message in your question would not be returned. It must have been from different XML or XSD documents.
As Michael Kay states in the comments:
elementFormDefault only affects local elements declarations.
elementFormDefault only comes into play when the XSD has a targetNamespace.
For more details about elementFormDefault see this answer.
and i want to return and array of buyers this is my xsd file, there is also the definition of buyer
in the xsd file:
<xs:element name="getBuyerResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="buyer" type="tns:buyer" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="deleteBuyerRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="deleteBuyerResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="response" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="buyer">
<xs:sequence>
<xs:element name="id" type="xs:int" />
<xs:element name="name" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
</xs:sequence>
</xs:complexType>
i dont know how to write the request and response for all the buyers in the application.
I am using spring boot,
java 7,
maven,
posgress,
Thanks
You can use the maxOccurs attribute. For example, to return any amount of buyer elements in your getBuyerResponse:
<xs:element name="getBuyerResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="buyer" type="tns:buyer" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Or to return a maximum of 8 buyer elements:
<xs:element name="getBuyerResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="buyer" type="tns:buyer" maxOccurs="8"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Note that maxOccurs defaults to 1 if you do not specify it.