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>
Related
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 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.
Is it possible to create the extended xsd with xslt. I get an xsd from another part and want to add some elements to the xsd but I cant get it to work. Is xslt the way to go or are there some better tools?
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.3.19">
<xs:element name="Message">
<xs:annotation>
<xs:documentation>Root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:choice>
<xs:element ref="addAddress"/>
<xs:element ref="addAddressResponse"/>
<xs:element ref="addEmailAddress"/>
<xs:element ref="addEmailAddressResponse"/>
</xs:choice>
</xs:element>
</xs:schema>
Extended:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.3.19">
<xs:element name="Message">
<xs:annotation>
<xs:documentation>Root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<!-- Extended field -->
<xs:element name="generalResponse" minOccurs="0">
<xs:annotation>
<xs:documentation>This response </xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="plainText" type="xs:string" minOccurs="0"/>
<xs:element name="invalidParameters" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="errorType" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:choice>
<xs:element ref="addAddress"/>
<xs:element ref="addAddressResponse"/>
<xs:element ref="addEmailAddress"/>
<xs:element ref="addEmailAddressResponse"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I solved it with this.
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="xs:element[#name='Message']/xs:complexType">
<xsl:copy>
<xs:sequence>
<!-- Extended field -->
<xs:element name="generalResponse" minOccurs="0">
<xs:annotation>
<xs:documentation>This response</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="plainText" type="xs:string" minOccurs="0" />
<xs:element name="invalidParameters" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="errorType" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xsl:apply-templates select="node()" />
</xs:sequence>
</xsl:copy>
</xsl:template>
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.
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.