How can I create all possible XMLs of these XSD:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://beep2000/client/beep.de" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://beep2000/client/beep.de">
<xsd:complexType name="TextType">
<xsd:sequence>
<xsd:element name="Text" minOccurs=1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="StreetType">
<xsd:sequence>
<xsd:element name="Street" type="xsd:string" minOccurs="1"/>
<xsd:element name="HouseNumber" type="xsd:int" minOccurs="1"/>
<xsd:element name="Suffix" type="xsd:string" minOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1"/>
<xsd:enumeration value="A"/>
<xsd:enumeration value="B"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BodyType">
<xsd:sequence>
<xsd:element name="FirstPart" type="TextType" minOccurs="1"/>
<xsd:element name="SecondPart" type="TextType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AdressType">
<xsd:sequence>
<xsd:element name="Street" type="StreetType" minOccurs="0">
<xsd:element name="PostalCode" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="5"/>
<xsd:pattern value="[0-9]{5}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="City" type="xsd:string" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RequestEnvelope">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" minOccurs="0"/>
<xsd:element name="Forename" type="xsd:string" minOccurs="1"/>
<xsd:element name="Surname" type="xsd:string" minOccurs="0"/>
<xsd:element name="PersonalID" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Adress" type="AdressType" minOccurs="0"/>
<xsd:element name="Body" type="BodyType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ResponseEnvelope">
<xsd:sequence>
<xsd:element name="Result" minOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="3"/>
<xsd:enumeration value="OK"/>
<xsd:enumeration value="NOK"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Errorcode" type="xsd:integer" minOccurs="0"/>
<xsd:element name="Errortext" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Comment" type="TextType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="MsgResponse" type="ResponseEnvelope"/>
<xsd:element name="MsgRequest" type="RequestEnvelope"/>
</xsd:schema>
There are some fields which are optional and some field which are mandatory. So there is a variety of XMLs. How can I find all possible structures of XMLs?
I expect something like:
XML No.1:
Field 1 --> mandatory
Field 2 --> mandatory
Field 3 --> optional
......
XML No.2.:
Field 1 --> mandatory
Field 2 --> mandatory
Field 4 --> mandatory
.....
Generating all possible XML files that conform to this schema would take an infinite amount of time and they would occupy an infinite amount of space.
However, you can generate a selection of sample documents using tools such as those described here:
How to generate sample XML documents from their DTD or XSD?
Related
I have 2 xmls : Basically two XSD schemas
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="my_export_file">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row">
<xsd:complexType>
<xsd:attributeGroup ref="rowattr" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attributeGroup ref="docelattr" />
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="rowattr">
<xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
**<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**
<xsd:attribute name="src_system_id" type="Str.80" use="required" />
</xsd:attributeGroup>
<xsd:attributeGroup name="docelattr">
<xsd:attribute name="reporting_date" type="xsd:string" />
<xsd:attribute name="interface_type" type="xsd:string" />
</xsd:attributeGroup>
<xsd:simpleType name="Str.1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Str.80">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="80" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
and
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="my_export_file">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row">
<xsd:complexType>
<xsd:attributeGroup ref="rowattr" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attributeGroup ref="docelattr" />
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="rowattr">
<xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
<xsd:attribute name="src_system_id" type="Str.80" use="required" />
**<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**
</xsd:attributeGroup>
<xsd:attributeGroup name="docelattr">
<xsd:attribute name="reporting_date" type="xsd:string" />
<xsd:attribute name="interface_type" type="xsd:string" />
</xsd:attributeGroup>
<xsd:simpleType name="Str.1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Str.80">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="80" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
I want to compare those and give differences. My code works fine but the only issue is if order of attributes is different it doesn't treat them as "SIMILAR". As you can see in the example, my xmls are same with just one change - order of object_level_ind is different. I want my code to not return this difference.
Code
var fis1 = new FileReader("C:\\Users\\test1.xsd ");
var fis2 = new FileReader("C:\\Users\\test2.xsd");
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(fis1,fis2));
diff.overrideElementQualifier(new ElementNameAndTextQualifier());
List<?> allDifferences = diff.getAllDifferences();
System.out.println(allDifferences);
I Also tried:
DifferenceEvaluator evaluator = DifferenceEvaluators
.downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_SEQUENCE);
Diff diff = DiffBuilder.compare(fis1)
.withTest(fis2).ignoreComments()
.ignoreWhitespace()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
.withDifferenceEvaluator(evaluator)
.checkForSimilar()
.build();
System.out.println("Differences: " + diff);
I Also tried solution given in comparing two xmls using xmlunit ignorng their order
But for my xml it gives:
identical: false
similar : false
Please let me know if any pointers.
Best Regards,
Abhi
This is happening because setIgnoteAttributeOrder ignores the order of the attributes of a node/element and not the actual order of node/element in the XML document. So, while <xsd:attribute name="object_level_ind" type="Str.1" use="optional" /> and <xsd:attribute use="optional" name="object_level_ind" type="Str.1" /> are considered same, the order of the elements is not ignored.
This answer here may have more details on how to compare ignoring element order - Compare two XML strings ignoring element order
I am trying to marshall a child element object to get xml string. And then using this xml string I would like to Unmarshall as well.
To clarify, I have generated my jaxb classes from xsd and I don't have any method in ObjectFactory that will provide the desired object.
The xsd defination: IBUSStatusType is a child of root element IBUSMessage
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://soa.myhealth.com/ibus/v1.1.0" targetNamespace="http://soa.myhealth.com/ibus/v1.1.0" elementFormDefault="qualified">
<xsd:element name="IBUSMessage" type="tns:IBUSMessage"/>
<xsd:complexType name="IBUSMessage">
<xsd:sequence>
<xsd:element name="Header" type="tns:IBUSHeaderType"/>
<xsd:element name="Detail" type="tns:IBUSVariantType"/>
<xsd:element name="Status" type="tns:IBUSStatusType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="IBUSHeaderType">
<xsd:sequence>
<xsd:element name="Retry" type="tns:RetryType" minOccurs="0"/>
<xsd:element name="ReplyToStack" type="tns:ReplyToStackType" minOccurs="0" maxOccurs="1"/>
<xsd:element name="TransactionContext" type="tns:TransactionContextType"/>
<xsd:element name="systemType" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="TestEnvironment" type="tns:TestEnvironmentType" minOccurs="0"/>
<xsd:element name="TransactionSetHeader" type="tns:TransactionHeaderType" minOccurs="0"/>
<xsd:element name="BeginningSegment" type="tns:TransactionSegmentType" minOccurs="0"/>
<xsd:element name="LegacySources" type="tns:SourceType" minOccurs="0"/>
<xsd:element name="Variables" type="tns:IBUSVariantType" minOccurs="0" maxOccurs="1"/>
<xsd:element name="RoutingTable" type="tns:RoutingTableType" minOccurs="0" maxOccurs="1"/>
<xsd:element name="OptionalHeaders" type="tns:IBUSVariantType" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="TestEnvironmentType">
<xsd:sequence>
<xsd:element name="useStubbedService" type="xsd:string" default="false"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="TransactionContextType">
<xsd:sequence>
<xsd:element name="source" type="xsd:string" minOccurs="0"/>
<xsd:element name="actionName" type="xsd:string"/>
<xsd:element name="serviceName" type="xsd:string"/>
<xsd:element name="ActionClass" type="tns:ActionClassType" minOccurs="0"/>
<xsd:element name="MessageType" type="tns:MessageResponseType" minOccurs="0"/>
<xsd:element name="processDate" type="xsd:date" minOccurs="0"/>
<xsd:element name="processTime" type="xsd:time" minOccurs="0"/>
<xsd:element name="userId" type="xsd:string" minOccurs="0"/>
<xsd:element name="unitOfWorkId" type="xsd:string" minOccurs="0"/>
<xsd:element name="operationId" type="xsd:string" minOccurs="0"/>
<xsd:element name="parentOperationId" type="xsd:string" minOccurs="0"/>
<xsd:element name="uowGroupIndicator" type="xsd:string" minOccurs="0"/>
<xsd:element name="uowGroupSize" type="xsd:string" minOccurs="0"/>
<xsd:element name="transactionId" type="xsd:string" minOccurs="0"/>
<xsd:element name="responseProtocol" type="xsd:string" minOccurs="0"/>
<xsd:element name="aggregation" type="xsd:string" minOccurs="0"/>
<xsd:element name="actionType" type="xsd:string" minOccurs="0"/>
<xsd:element name="origSource" type="xsd:string" minOccurs="0"/>
<xsd:element name="origSourceClass" type="xsd:string" minOccurs="0"/>
<xsd:element name="requestIdentifier" type="xsd:string" minOccurs="0"/>
<xsd:element name="generatedInterchangeControlNumber" type="xsd:string" minOccurs="0"/>
<xsd:element name="generatedGroupControlNumber" type="xsd:string" minOccurs="0"/>
<xsd:element name="generatedTransactionControlNumber" type="xsd:string" minOccurs="0"/>
<xsd:element name="messageTimeout" type="xsd:string" minOccurs="0"/>
<xsd:element name="ServiceVersion" type="tns:VersionType" minOccurs="0"/>
<xsd:element name="IBUSVersion" type="tns:VersionType" fixed="1.1.0"/>
</xsd:sequence>
</xsd:complexType>
<!-- X12 Transactions ONLY -->
<xsd:complexType name="TransactionHeaderType">
<xsd:sequence>
<xsd:element name="transactionSetIdentifierCode" type="xsd:string" minOccurs="0"/>
<xsd:element name="transactionSetControlNumber" type="xsd:string" minOccurs="0"/>
<xsd:element name="conventionReferenceNumber" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<!-- X12 Transactions ONLY -->
<xsd:complexType name="TransactionSegmentType">
<xsd:sequence>
<xsd:element name="structureCode" type="xsd:string" minOccurs="0"/>
<xsd:element name="transactionIdentifier" type="xsd:string" minOccurs="0"/>
<xsd:element name="transactionSetCreationDate" type="xsd:date" minOccurs="0"/>
<xsd:element name="transactionSetCreationTime" type="xsd:time" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="SourceType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="action" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="IBUSVariantType">
<xsd:sequence>
<xsd:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ReplyToStackType">
<xsd:sequence>
<xsd:element name="ReplyTo" type="tns:ReplyToStackEntry" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ReplyToStackEntry">
<xsd:sequence>
<xsd:element name="replyToQ" type="xsd:string"/>
<xsd:element name="replyToQMgr" type="xsd:string"/>
<xsd:element name="requestIdentifier" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="IBUSStatusType">
<xsd:sequence>
<xsd:element name="code" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="Messages" type="tns:MessagesType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="MessagesType">
<xsd:sequence>
<xsd:element name="Message" type="tns:StatusMessageType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="StatusMessageType">
<xsd:sequence>
<xsd:element name="Reason" type="tns:StatusPairType"/>
<xsd:element name="type" type="xsd:string"/>
<xsd:element name="FollowUpAction" type="tns:StatusPairType" minOccurs="0"/>
<xsd:element name="Context" type="tns:StatusPairType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="StatusPairType">
<xsd:sequence>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="code" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ActionClassType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value=""/>
<xsd:enumeration value="GUI"/>
<xsd:enumeration value="BATCH"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="MessageResponseType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="ServiceRequest"/>
<xsd:enumeration value="ServiceResponse"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="VersionType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="RoutingTableType">
<xsd:sequence>
<xsd:element name="Route" type="tns:RouteType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RouteType">
<xsd:sequence>
<xsd:element name="key" type="xsd:string"/>
<xsd:element name="routeOverride" type="xsd:string" minOccurs="0"/>
<xsd:element name="routeAppend" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RetryType">
<xsd:sequence>
<xsd:element name="count" type="xsd:long" minOccurs="0"/>
<xsd:element name="delay" type="xsd:long" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
I want to marshall an object of IBUSStatusType. To do that my code for marshalling as follows:
public static StringWriter getStringFromIBUSStatusType(IBUSStatusType iBUSStatusType) throws JAXBException
{
StringWriter stringWriter = new StringWriter();
JAXBContext jaxbContext = JAXBContext
.newInstance(com.myhealth.soa.ibus.v1_1.ObjectFactory.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
new DefaultNamespacePrefixMapper());
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
QName qName = new QName("com.myhealth.soa.ibus.v1_1", "iBUSStatusType");
JAXBElement<IBUSStatusType> root = new JAXBElement<>(qName, IBUSStatusType.class, iBUSStatusType);
marshaller.marshal(root, stringWriter);
return stringWriter;
}
I get this xml string when I do marshall IBUSStatusType object:
<ns2:iBUSStatusType xmlns:tns="http://soa.myhealth.com/ibus/v1.1.0" xmlns:ns2="com.myhealth.soa.ibus.v1_1"><tns:code>8</tns:code><tns:description>Completed With Warning</tns:description><tns:Messages><tns:Message><tns:Reason><tns:description>Subscriber's email missing</tns:description><tns:code>2187</tns:code></tns:Reason><tns:type>1</tns:type><tns:Context><tns:description>WARN</tns:description><tns:code>Policy=MissingSubscriberEmail</tns:code></tns:Context></tns:Message></tns:Messages></ns2:iBUSStatusType>
Well, now I want to Unmarshall this xml string I got from marshal process. And the code for Unmarshall:
public static com.myhealth.soa.ibus.v1_1.IBUSStatusType getIBUSStatusType(String xml)
throws JAXBException
{
JAXBContext jaxbContext = JAXBContext
.newInstance(com.myhealth.soa.ibus.v1_1.ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return ((JAXBElement<IBUSStatusType>) jaxbUnmarshaller
.unmarshal(new StringReader(xml))).getValue();
}
I get this error when I do unmarshall on the generated xml string:
javax.xml.bind.UnmarshalException: unexpected element (uri:"com.myhealth.soa.ibus.v1_1", local:"iBUSStatusType"). Expected elements are <{http://soa.myhealth.com/ibus/v1.1.0}IBUSMessage>
Now provided that my namespaces are:
put("http://soa.myhealth.com/common/v1", "cmn");
put("http://soa.myhealth.com/MemberService/v1", "mems");
put("http://soa.myhealth.com/ibus/v1.1.0", "tns");
put("http://soa.myhealth.com/member/enrollment/v1", "menr");
Just look at your schema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://soa.myhealth.com/ibus/v1.1.0" targetNamespace="http://soa.myhealth.com/ibus/v1.1.0" elementFormDefault="qualified">
<xsd:element name="IBUSMessage" type="tns:IBUSMessage"/>
<xsd:complexType name="IBUSMessage">
<xsd:sequence>
<xsd:element name="Header" type="tns:IBUSHeaderType"/>
<xsd:element name="Detail" type="tns:IBUSVariantType"/>
<xsd:element name="Status" type="tns:IBUSStatusType"/>
</xsd:sequence>
</xsd:complexType>
There is a namespace http://soa.myhealth.com/ibus/v1.1.0
And when you do unmarsalling it expects http://soa.myhealth.com/ibus/v1.1.0
but previously when you do marshalling you put the following namespace
QName qName = new QName("com.myhealth.soa.ibus.v1_1", "iBUSStatusType");
They are different.
Well, finally we made a solution. I am pasting my solution bellow. I did change the code according to #Iurii but the problem was somewhere else.
So, this is the change in Marshalling part. The 2nd paramater of QName must be the class name NOT the object of it. I tried to follow this link https://codenotfound.com/jaxb-marshal-element-missing-xmlrootelement-annotation.html but did not work. When changed the 2nd parameter of QName to Class name then it worked.
And the context should be made out of that class because ObjectFactory does not have it.
JAXBContext jaxbContext = JAXBContext.newInstance(IBUSStatusType.class);
QName qName = new QName("http://soa.independenthealth.com/ibus/v1.1.0", "IBUSStatusType");
JAXBElement<IBUSStatusType> root = new JAXBElement<IBUSStatusType>(qName, IBUSStatusType.class, iBUSStatusType);
And this is the whole Unmarshal code. We had to change the xml to node before unmarshall it. The import of Element is import org.w3c.dom.Element;
public static IBUSStatusType getIBUSStatusType(String xml)
throws JAXBException, SAXException, IOException, ParserConfigurationException
{
JAXBContext jaxbContext = JAXBContext
.newInstance(IBUSStatusType.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Element node = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new ByteArrayInputStream(xml.getBytes()))
.getDocumentElement();
return ((JAXBElement<IBUSStatusType>) jaxbUnmarshaller.unmarshal(node, IBUSStatusType.class)).getValue();
}
The error when validated using SAX parser is:
"org.xml.sax.SAXParseException; systemId: file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd; lineNumber: 10; columnNumber: 31; src-resolve.4.1: Error resolving component 'name'.
It was detected that 'name' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'.
If 'name' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'name' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'."
address.xml
<?xml version ="1.0" encoding="UTF-8"?>
<address
xmlns:personal="Personal things"
xmlns:houses="Regarding to houses"
xmlns="http://www.w3schools.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
xsd:schemaLocation="address.xsd"
>
<name>
<personal:title>Mr.</personal:title>
<first-name>Samitha</first-name>
<last-name>Chathuranga</last-name>
</name>
<sssd></sssd>
<house-id>
<houses:title>107 B</houses:title>
<NAME>Sam's Home</NAME>
<!-- An intnal entity is used for the single quote in House Name here-->
</house-id>
<village>Poramba</village>
<city district="Galle" province="Southern">AG</city>
<postal-code>80300</postal-code>
<country>Sri Lanka</country>
</address>
address.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/SampleSchema"
xmlns:tns="http://www.example.org/SampleSchema"
>
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="name" />
<xsd:element ref="house-id" />
<xsd:element ref="village" />
<xsd:element ref="city" />
<xsd:element ref="postal-code" />
<xsd:element ref="country" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="name">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="title" />
<xsd:element ref="first-name" />
<xsd:element ref="last-name" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
<xsd:element name="house-id">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="title" />
<xsd:element ref="NAME" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="NAME" type="xsd:string" />
<xsd:element name="village" type="xsd:string" />
<xsd:element name="country" type="xsd:string" />
<xsd:element name="city">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="2" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="postal-code">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
How to resolve this error?
Well your xsd is invalid. You are constructing it wrong. Here are my suggestions. Download XMLSpy or Liquid XML studio and then view my examples to see how to construct a proper XSD and then validate the XSD against it. These editors are a great way to visually see the XML documents and xsd's. They will help a lot.
Essentially you need to declare your types in the XSD then create elements based on those types. While your approach can work I would suggest that you study the approach I have taken it is pretty easy. I could explain it all but I think you will get the idea rather quickly.
Address.xsd
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com)-->
<xsd:schema xmlns:address="http://www.example.org/AddressSchema"
targetNamespace="http://www.example.org/AddressSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person_name"
type="address:person_name_type" />
<xsd:element name="ssd"
type="address:ssd_type" />
<xsd:element name="house_id"
type="address:house_id_type" />
<xsd:element name="village"
type="address:village_type" />
<xsd:element name="city"
type="address:city_type" />
<xsd:element name="postal_code"
type="address:postalcode_type" />
<xsd:element name="country"
type="address:country_type" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="postalcode_type">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="city_type">
<xsd:restriction base="xsd:string">
<xsd:length value="2" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="village_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="country_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="title_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="first_name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="last_name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:complexType name="house_id_type">
<xsd:sequence>
<xsd:element name="title"
type="address:title_type" />
<xsd:element name="name"
type="address:name_type" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="person_name_type">
<xsd:sequence>
<xsd:element name="title"
type="address:title_type" />
<xsd:element name="first_name"
type="address:first_name_type" />
<xsd:element name="last_name"
type="address:last_name_type" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ssd_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>
Address.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com) -->
<address xsi:schemaLocation="http://www.example.org/AddressSchema D:\GroundZero\address.xsd" xmlns="http://www.example.org/AddressSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<person_name>
<title>string</title>
<first_name>string</first_name>
<last_name>string</last_name>
</person_name>
<ssd>string</ssd>
<house_id>
<title>string</title>
<name>string</name>
</house_id>
<village>string</village>
<city>AB</city>
<postal_code>80300</postal_code>
<country>string</country>
</address>
I would also note that your instance document has the root (address) element in namespace http://www.w3schools.com, while your schema defines an address element in namespace http://www.example.org/AddressSchema. You can't just sprinkle namespaces around like fairy-dust and hope they add glamour to your code; they are fundamental and you need to get them right.
I will start with an example
<template>
<components>
<component name="switch" />
<component name="server" />
</components>
<layout>
<grid>
<position componentName="switch" positionX="0" positionY="0" />
</grid>
</layout>
</template>
What I want is to restrict values in componentName attribute to match one of the names specified above in components. Is this possible in JAXB? Because I need to have annotated classes which are then used to generate XSD.
Given your scenario, XSD 1.0 can enforce your "referential integrity" through a key/keyref combo. However, I am not aware of what annotations there are for these constructs in JAXB (it sounds as if you're looking at generating an XSD from you Java classes); at least I never ran into such annotations (see the list here)
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="template">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="components">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="component">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="layout">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="grid">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="position">
<xsd:complexType>
<xsd:attribute name="componentName" type="xsd:string" use="required" />
<xsd:attribute name="positionX" type="xsd:unsignedByte" use="required" />
<xsd:attribute name="positionY" type="xsd:unsignedByte" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="ComponentsKey">
<xsd:selector xpath="components/component"/>
<xsd:field xpath="#name"/>
</xsd:key>
<xsd:keyref name="MatchComponent" refer="ComponentsKey">
<xsd:selector xpath="layout/grid/position"/>
<xsd:field xpath="#componentName"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>
I have a WSDL document:
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://java.CAEServer"
xmlns:server="http://caeserver.caebeans.org"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://java.CAEServer"
name="CAEInstanceImplService">
<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://caeserver.caebeans.org"
xmlns:test="http://example.org"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:serv="http://caeserver.caebeans.org"
attributeFormDefault="unqualified" elementFormDefault="qualified">
<xsd:element name="data" type="xsd:string"/>
<xsd:element name="parameter">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="text">
<xsd:complexType>
<xsd:attribute name="data" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="units">
<xsd:complexType>
<xsd:attribute name="data" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="comment">
<xsd:complexType>
<xsd:attribute name="data" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="enums" type="xsd:string"/>
<xsd:element name="default" type="xsd:int"/>
<xsd:element name="value" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="type" type="xsd:string"/>
<xsd:attribute name="visible" type="xsd:boolean"/>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="languageData">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="name" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:element name="language">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="data" type="serv:languageData"/>
</xsd:sequence>
<!--<xsd:attribute name="xml:lang" type="xsd:string"/>-->
</xsd:complexType>
</xsd:element>
<xsd:element name="instance_ID" type="xsd:string"/>
<xsd:element name="problemCaebean">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="categories">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="category">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" ref="serv:parameter"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="data" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="resources">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" ref="serv:language"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="author" type="xsd:string"/>
<xsd:attribute name="version" type="xsd:decimal"/>
<xsd:attribute name="caebeanId" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="caeProject">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="serv:problemCaebean"/>
<xsd:element name="logical">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="node">
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="type" type="xsd:string"/>
<xsd:attribute name="condition" type="xsd:string"/>
<xsd:attribute name="execute" type="xsd:string"/>
<xsd:attribute name="x" type="xsd:int"/>
<xsd:attribute name="y" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
<xsd:element maxOccurs="unbounded" name="edge">
<xsd:complexType>
<xsd:attribute name="source" type="xsd:string"/>
<xsd:attribute name="target" type="xsd:string"/>
<xsd:attribute name="when" type="xsd:boolean"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="physical">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="componentCaebean">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="input">
<xsd:complexType>
<xsd:attribute name="filename" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element maxOccurs="unbounded" name="output">
<xsd:complexType>
<xsd:attribute name="filename" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="jar-premode">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="exec">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="param">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="appname" type="xsd:string"/>
<xsd:attribute name="appversion" type="xsd:decimal"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="author" type="xsd:string"/>
<xsd:attribute name="version" type="xsd:decimal"/>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Status">
<xsd:complexType>
<xsd:choice>
<xsd:element name="NOT_STARTED"/>
<xsd:element name="RUNNING"/>
<xsd:element name="HELD"/>
<xsd:element name="SUCCESSFULL"/>
<xsd:element name="FAILED"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:element name="CurrentTime">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:dateTime">
<xsd:anyAttribute namespace="##other" processContents="lax"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="TerminationTime" nillable="true">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:dateTime">
<xsd:anyAttribute namespace="##other" processContents="lax"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="Description">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="serv:Status"/>
<xsd:element ref="serv:TerminationTime"/>
<xsd:element ref="serv:CurrentTime"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!--
message used to create a new instance WS-Resource
-->
<xsd:element name="createInstance">
<xsd:complexType/>
</xsd:element>
<!--
the response, simply contains an EPR to the new instance
-->
<xsd:element name="createInstanceResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="instanceID" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="submitJob">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="serv:instance_ID"/>
<xsd:element ref="serv:problemCaebean"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="submitJobResponse">
<xsd:complexType>
<xsd:sequence>
<!--<xsd:element name="xml" type="xsd:string"/>-->
<xsd:element name="Result" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="getStatus">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="instance_ID" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="getStatusResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="serv:Status"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</types>
<message name="SubmitJobRequest">
<part name="parameter" element="server:submitJob"/>
</message>
<message name="SubmitJobResponse">
<part name="parameter" element="server:submitJobResponse"/>
</message>
<message name="GetStatusRequest">
<part name="parameter" element="server:getStatus"/>
</message>
<message name="GetStatusResponse">
<part name="parameter" element="server:getStatusResponse"/>
</message>
<portType name="CAEInstance">
<operation name="SubmitJob">
<input message="tns:SubmitJobRequest"/>
<output message="tns:SubmitJobResponse"/>
</operation>
<operation name="GetStatus">
<input message="tns:GetStatusRequest"/>
<output message="tns:GetStatusResponse"/>
</operation>
</portType>
<binding name="CAEInstanceImplPortBinding" type="tns:CAEInstance">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="SubmitJob">
<soap:operation soapAction="http://java.CAEServer/submitJob"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="GetStatus">
<soap:operation soapAction="http://java.CAEServer/getStatus"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="CAEInstanceImplService">
<port name="CAEInstanceImplPort" binding="tns:CAEInstanceImplPortBinding">
<soap:address location="http://127.0.0.1:9000/caeinstance"/>
</port>
</service>
</definitions>
And I'm creating types and services definitions with command
wsimport -keep -verbose ../src/main/resources/instance.wsdl
But when I compiled and runned my project and tryed to compile client stubs from generated WSDL, I've got an error
parsing WSDL...
[ERROR] undefined element declaration 'ns1:instance_ID'
line 36 of http://localhost:9000/caeinstance?xsd=1
[ERROR] undefined element declaration 'ns1:Result'
line 49 of http://localhost:9000/caeinstance?xsd=1
What's wrong with my WSDL?
CXF and Eclipse both supply WSDL validators. You should run your WSDL through one of those and see what it says.
Did you create that WSDL?
I think you are trying to use some data types which are not exported in your WSDL.
The wsdl refers to a schema named 's' but this can't be found, because its URL is
XMLSchema but should be
XMLSchema.xsd
U can use
wsimport -b http://www.w3.org/2001/XMLSchema.xsd usrtowsdl?WSDL