I use JAXB to generate Java classes from XSD, JAXB unable to identify the group element from included xsd, simple elements works fine. Error : org.xml.sax.SAXParseException: undefined simple or complex type
<?xml version="1.0" encoding="UTF-8"?>
<!-- Schema for itinerary response -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.cleartrip.com/air/" xmlns:air="http://www.cleartrip.com/air/">
<xs:include schemaLocation="air-common.xsd"/>
***<xs:element name="itinerary" type="air:TripType"/>***
</xs:schema>
air-common.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.cleartrip.com/air/"
xmlns:air="http://www.cleartrip.com/air/">
<xs:simpleType name="NameType">
<xs:annotation>
<xs:documentation>Defining a type for a name</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="27"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="AirportCodeType">
<xs:annotation>
<xs:documentation>Airport codes</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z0-9]{3}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="PhoneNumberType">
<xs:annotation>
<xs:documentation>Phone numbers</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
***<xs:group name="TripType">
<xs:annotation>
<xs:documentation>Trip type -- used to display an itinerary or trip</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element minOccurs="0" ref="air:trip-id"/>
<xs:element minOccurs="0" ref="air:created-at"/>
<xs:element ref="air:pricing-summary"/>
<xs:element ref="air:flights"/>
<xs:element ref="air:pax-pricing-info-list"/>
<xs:element ref="air:pax-info-list"/>
<xs:element ref="air:contact-detail"/>
<xs:group minOccurs="0" ref="air:PaymentDetailResponse"/>
<xs:element minOccurs="0" ref="air:ancillary-data"/>
</xs:sequence>
</xs:group>***
</xs:schema>
Code generation works fine with all simple type, problem is only with xs:group name="TripType", what might be the problem.
Related
I have the a little problem with the xsd below. I have created a type of vaultobject with a type attribute that can have any value in a enumeration. Then I derived VaultServiceObject from vaultobject en set a restriction to limit type to a fixed value for the derived object.
The editor (liquid-xml) design service seems to understand this and displays correctly, but the text editor marks the line 26 as and error.
<xs:attribute name="Type" fixed="ServiceConfiguration" type="xs:string" use="required">
Saying "Error Invalid attribute restriction. Derived attribute's type is not a valid restriction of the base attribute's type." So from this I guess "xs:string" is wrong. But I cannot figure out what type I should use.
Hopefully there is someone more experienced with XSD out there.
p.s I cobbled xsd below together from several other similar stackoverflow questions, but they do not supply this exact combination and its solution. So please do not point to those without explaining what I am looking for.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="VaultObject">
<xs:sequence>
<xs:annotation>
<xs:documentation>Allows for derived object to have a sequence of elements</xs:documentation>
</xs:annotation>
</xs:sequence>
<xs:attribute name="Type" use="required">
<xs:annotation>
<xs:documentation>This is the list of possible vault objects. Derived objects need to lock this down to the object type the represent.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Merge" />
<xs:enumeration value="ServiceConfiguration" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:anyAttribute />
</xs:complexType>
<xs:complexType name="VaultServiceConfigurationObject">
<xs:complexContent>
<xs:restriction xmlns:q2="http://www.it-workz.nl/IDM" base="VaultObject">
<xs:attribute name="Type" fixed="ServiceConfiguration" type="xs:string" use="required">
<xs:annotation>
<xs:documentation xml:lang="EN">This property is inherited from VaultObject, but is locked down to the fixed value of "ServiceConfiguration"</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="ServiceType">
<xs:annotation>
<xs:documentation xml:lang="EN">The list of possible service types we support. Derived service definitions need to lock this down to a single value in their own type.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="SystemA" />
<xs:enumeration value="SystemB" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="Name" type="xs:string" use="required">
<xs:annotation>
<xs:documentation xml:lang="EN">Every service needs to be uniquely named. Even between different service types.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>
You can do this by breaking the 'Type' enum out into it's own SimpleType.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="VaultObjectType">
<xs:restriction base="xs:string">
<xs:enumeration value="Merge" />
<xs:enumeration value="ServiceConfiguration" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="VaultObject">
<xs:sequence>
<xs:annotation>
<xs:documentation>Allows for derived object to have a sequence of elements</xs:documentation>
</xs:annotation>
</xs:sequence>
<xs:attribute name="Type" type="VaultObjectType" use="required">
<xs:annotation>
<xs:documentation>This is the list of possible vault objects. Derived objects need to lock this down to the object type the represent.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:anyAttribute />
</xs:complexType>
<xs:complexType name="VaultServiceConfigurationObject">
<xs:complexContent>
<xs:restriction xmlns:q2="http://www.it-workz.nl/IDM" base="VaultObject">
<xs:attribute name="Type" fixed="ServiceConfiguration" type="VaultObjectType" use="required">
<xs:annotation>
<xs:documentation xml:lang="EN">This property is inherited from VaultObject, but is locked down to the fixed value of "ServiceConfiguration"</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="ServiceType">
<xs:annotation>
<xs:documentation xml:lang="EN">The list of possible service types we support. Derived service definitions need to lock this down to a single value in their own type.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="SystemA" />
<xs:enumeration value="SystemB" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="Name" type="xs:string" use="required">
<xs:annotation>
<xs:documentation xml:lang="EN">Every service needs to be uniquely named. Even between different service types.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>
I wanted to know the proper way to do an xsd as certain nodes in my xsd may be reused in other xsd's. Like in Search Item request I will use
<xs:element name="Item">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Price" type="xs:double"/>
<xs:element name="SupplierCode" type="xs:string"/>
<xs:element name="Supplier" type="xs:string"/>
<xs:element name="SupplierName" type="xs:string"/>
<xs:element name="Manufacturer" type="xs:string"/>
<xs:element ref="CustomerReviews" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="model" type="xs:string" use="required"/>
<xs:attribute name="href" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
then I might need to use it in another xsd.
So should I repeat it or make a xsd with it in it and reference it?
My Current xsd with include
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://www.itwholesaledeluxe.com" targetNamespace="http://www.itwholesaledeluxe.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Item.xsd"/>
<xs:element name="Search-Item-Request">
<xs:complexType>
<xs:sequence>
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="Item"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Then the item xsd partially since its too big to post
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.itwholesaledeluxe.com">
<xs:complexType name="Item">
<xs:sequence>
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</sequence>
</complexType>
</xs:schema>
follow DRY: If it's the same object (in the same namespace): make an xsd and reference it.
That way, if the object is altered at some point, you only need to make your changes once. Also, any code generated for this object only needs to be generated once.
Use the following xsd's as a reference:
Main.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns1="http://www.itwholesaledeluxe.com" targetNamespace="http://www.itwholesaledeluxe.com"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Item.xsd" />
<xs:element name="Search-Item-Request">
<xs:complexType>
<xs:sequence>
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="ns1:Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Item.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://www.itwholesaledeluxe.com">
<xs:complexType name="Item">
<xs:sequence>
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
All, I have an XML doc which I don't control for which I need to create an xsd to validate. The XML doc has multiple transaction types, some of which are required a specific number of times, and some aren't. the parent element is simply <transaction>, the child element can be either a <ControlTransaction> or a <RetailTransaction>. The issue is that I need to require a <transaction> to exists with a <ControlTransaction> with a <ReasonCode> element having a value of "Register Open" and another with a value of "Register Close" as follows:
<?xml version="1.0" encoding="UTF-8"?>
<RegisterDay xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cp="urn:register">
<Transaction>
<SequenceNumber>1</SequenceNumber>
<ControlTransaction>
<ReasonCode>Register Open</ReasonCode>
</ControlTransaction>
</Transaction>
<Transaction>
<SequenceNumber>2</SequenceNumber>
<RetailTransaction>
...stuff..
<Total>9.99</Total>
</RetailTransaction>
</Transaction>
<Transaction>
<SequenceNumber>3</SequenceNumber>
<ControlTransaction>
<ReasonCode>Register Close</ReasonCode>
</ControlTransaction>
</Transaction>
</RegisterDay>
My best attempt is to use types in my schema, but get "Elements with the same name and same scope must have the same type". I don't know how to get around this.
<?xml version="1.0"?>
<xs:schema
xmlns:cp="urn:register"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="RegisterDay">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="Transaction" type="TransactionRegisterOpen_type"/>
<xs:element minOccurs="1" maxOccurs="unbounded" name="Transaction" type="RetailTransaction_type"/>
<xs:element minOccurs="1" maxOccurs="1" name="Transaction" type="TransactionRegisterClose_type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="RegisterOpen_type">
<xs:restriction base="xs:string">
<xs:pattern value="Register Open"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="RegisterClose_type">
<xs:restriction base="xs:string">
<xs:pattern value="Register Close"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="TransactionRegisterOpen_type">
<xs:sequence>
<xs:element name="SequenceNumber" type="xs:unsignedShort"/>
<xs:element name="ControlTransaction">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="ReasonCode" type="RegisterOpen_type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TransactionRegisterClose_type">
<xs:sequence>
<xs:element name="SequenceNumber" type="xs:unsignedShort"/>
<xs:element name="ControlTransaction">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="ReasonCode" type="RegisterClose_type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RetailTransaction_type">
<xs:sequence>
<xs:element name="SequenceNumber" type="xs:unsignedShort"/>
<xs:element name="ControlTransaction">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="Total" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Has anyone run into this and/or have any suggestions? I'm pretty much stumped.
Perhaps with enumeration ?
<?xml version="1.0"?>
<xs:schema
xmlns:cp="urn:register"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="urn:register">
<xs:element name="RegisterDay">
<xs:complexType>
<xs:sequence>
<xs:element
minOccurs="1"
maxOccurs="unbounded"
name="Transaction"
type="cp:TypeTransaction"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="TypeTransaction">
<xs:sequence>
<xs:element name="SequenceNumber" type="xs:unsignedShort"/>
<xs:choice>
<xs:element name="RetailTransaction"/>
<xs:element name="ControlTransaction">
<xs:complexType>
<xs:sequence>
<xs:element name="ReasonCode">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Register Open"/>
<xs:enumeration value="Register Close"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:schema>
What would be the correct XML Schema 1.0 declaration for a
<notice xml:lang="en">Banana banana banana</notice>
where:
The xml:lang attribute is compulsory
The value "en" is fixed and compulsory
The content of notice is simple text.
The content of notice is fixed (as above) and compulsory?
My best (but wrong) effort is the following fragment:
<xs:element name="notice" use="required" fixed="Banana banana banana">
<xs:complexType>
<xs:simpleContent>
<xs:extension>
<xs:attribute ref="xml:lang" use="required" fixed="en"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://www.w3.org/XML/1998/namespace" />
<xs:element name="notice" type="notice"/>
<xs:complexType name="notice">
<xs:simpleContent>
<xs:extension base="CONTENT">
<xs:attribute ref="xml:lang" use="required" fixed="en"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="CONTENT">
<xs:restriction base="xs:string">
<xs:enumeration value="Banana banana banana"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
I have following XML schema:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="content" type="contentType"/>
<xs:complexType name="contentType">
<xs:complexContent>
<xs:extension base="versionedElementType">
<xs:sequence>
<xs:element name="item" type="itemType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="itemType" mixed="true">
<xs:complexContent>
<xs:extension base="itemTypeBase">
<xs:sequence>
<xs:element name="order" type="xs:unsignedInt"/>
<xs:element name="id" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- Simple type convert to complex type -->
<xs:complexType name="itemTypeBase" mixed="true">
<xs:simpleContent>
<xs:extension base="itemDescriptionType">
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- Simple type -string restriction -->
<xs:simpleType name="itemDescriptionType" >
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="64"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="versionedElementType">
<xs:attribute name="version" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
which I use to validate this XML instance (I want to mix the text in the 'item' element with sub-elements 'order' and 'id'):
<?xml version="1.0" encoding="UTF-8"?>
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Content.xsd"
version ="1.0">
<item>Description here...
<order>2</order>
<id>2</id>
</item>
</content>
Whatever I did the validation still says taht there is an error:
The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'itemType' is mixed, but its base type is not.
But I can see that both types - itemType and itemTypeBase are MIXED!!
Thanks a lot
STeN
First of all the error which I see if I open your schema in Visual Studio 2010 is:
The derived type and the based type
must have the same content type.
In you current schema the type itemTypeBase is defined with respect of the <xs:simpleContent> and derived type itemType with the respect of <xs:complexContent> which is not allowed. Either you allow no sub-elements and use <xs:simpleContent> or you do use child elements and use <xs:complexContent>.
I personally don't like and don't use mixed types. If I understand you correct you want to make some restrictions in the text from the content. You want to have the content length between 1 and 64 characters. But <order>2</order>, <id>2</id> and all whitespace, inclusive the new line characters, are also a part of the content. If you want that <item> has simple content, then you can not insert child elements inside.
So the pragmatical solution would be go away from the mixed model and use the XML document in the form
<?xml version="1.0" encoding="utf-8"?>
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Content.xsd"
version ="1.0">
<item>
<description>Description here...</description>
<order>2</order>
<id>2</id>
</item>
</content>
where Content.xsd is
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="content" type="contentType"/>
<xs:complexType name="contentType">
<xs:sequence>
<xs:element name="item" type="itemType"
minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="version" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="itemType">
<xs:sequence>
<xs:element name="description" type="itemDescriptionType"/>
<xs:element name="order" type="xs:unsignedInt"/>
<xs:element name="id" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="itemDescriptionType" >
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="64"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
All will be very simple and clear.