Parse types in XML schema. Java - java

In front of me there is a problem - a complete schema parse (with include, import, redefine). The problem, is not even parsing scheme, it's to resolve types from this schema.
To explain the problem, here is an example:
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myprefix="http://myhost.ru/service/" xmlns:anprefix="http://anotherhost.ru/pub-service/" targetNamespace="http://myhost.ru/service/">
<xs:import namespace="http://anotherhost.ru/pub-service/" schemaLocation="anotherhost_schema.xsd"/>
<xs:complexType>
<xs:sequence>
<xs:element name="ServiceCode" type="anprefix:ServiceCodeType">
<xs:annotation>
<xs:documentation>Service code</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="MessageClass" type="myprefix:MessageClassType"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="MessageClassType">
<xs:restriction base="xs:string">
<xs:enumeration value="REQUEST">
<xs:annotation>
<xs:documentation>Request from client
</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="RESPONSE">
<xs:annotation>
<xs:documentation>Response to client</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:schema>
anotherhost_schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:anprefix="http://anotherhost.ru/pub-service/" targetNamespace="http://anotherhost.ru/pub-service/">
<xs:simpleType name="ServiceCodeType">
<restriction base="xs:string">
</restriction>
</xs:simpleType>
</xs:schema>
Example is not very difficult, the real problem is much bigger. My problem is to parse schema and create it's internal representation (that form generator and request handler will use), for example, as follows:
{
"ServiceCode": {"string", String.class, "Service code"},
"MessageClass": {"string", {"REQUEST":"Request from client","RESPONSE":"Response to client"}, ""}
}
Inner representation can be various, but simple for use in Java. Is there such a library? I know, that there is a library, called JAXB, created specially for XML-parsing, but I don't understand how to bind it to my problem.

Related

XSD Validation on integer 0001 or 000

In my application, we use XSD validation to check whether the given text input is a number greater or equal to 0. This works fine, but when we enter numbers like 000 or 001 it validates it too. So when we write to a file it writes the 000 and 001 but we want to not validate them. We want them as 0 or 1, not 000 or 001.
How can we enable it and trim the 00 at the beginning? Is there an XSD type or restriction for it?
How can we solve this issue?
I am not sure if you only want to validate the content given to write or read from the files or also alter it. the xml schema cannot alter the data. Assuming the to be validated content is of the type string. You can use a regex pattern to check if the content is correct. the below example gives checks if the last character in the string is 0 or 1 and all the characters prior to it must be 0.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="input">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(0*[0-1])?[0-1]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
update after receiving comments
if you only want to approve the xml structure that has a single digit in the range of 0 to 9 you can use the below example.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="input">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:pattern value="[0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Cannot generate wsdl JAVA with wsimport for GetHotelMediaRQ

I Cannot generate wsdl JAVA with wsimport for GetHotelMediaRQ. Could you please tell me how to resolve the problem?
wsimport -target 2.1 -keep -d /Users/jinli/Tmp/ws/classes -s . -p org.jellylab.soap.sacs.proto 'http://webservices.sabre.com/wsdl/sabreXML1.0.00/VCMP/GetHotelMediaRQ_v2.0.0.wsdl'
parsing WSDL...
[ERROR] 'HotelMediaInfos' is already defined
line 51 of http://webservices.sabre.com/wsdl/sabreXML1.0.00/VCMP/GetHotelMediaRS_v2.0.0.xsd
[ERROR] (related to above error) the first definition appears here
line 390 of http://webservices.sabre.com/wsdl/sabreXML1.0.00/VCMP/HotelMediaCommons_v2.0.0.xsd
Exception in thread "main" com.sun.tools.internal.ws.wscompile.AbortException
at com.sun.tools.internal.ws.processor.modeler.wsdl.JAXBModelBuilder.bind(JAXBModelBuilder.java:129)
at com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModeler.buildJAXBModel(WSDLModeler.java:2283)
at com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModeler.internalBuildModel(WSDLModeler.java:183)
at com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModeler.buildModel(WSDLModeler.java:126)
at com.sun.tools.internal.ws.wscompile.WsimportTool.buildWsdlModel(WsimportTool.java:429)
at com.sun.tools.internal.ws.wscompile.WsimportTool.run(WsimportTool.java:190)
at com.sun.tools.internal.ws.wscompile.WsimportTool.run(WsimportTool.java:168)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.tools.internal.ws.Invoker.invoke(Invoker.java:159)
at com.sun.tools.internal.ws.WsImport.main(WsImport.java:42)
The error is correct, this was defined 2 times. You need to download the WSDL and all the associated schemas.
The GetHotelMediaRS_v2.0.0.xsd looks like below xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:stlp="http://services.sabre.com/STL_Payload/v02_02" xmlns="http://services.sabre.com/hotel/media/v2" targetNamespace="http://services.sabre.com/hotel/media/v2" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://services.sabre.com/STL_Payload/v02_02" schemaLocation="built-ins/STL2_Payload_v02_02.xsd" />
<xs:include schemaLocation="HotelMediaCommons_v2.0.0.xsd" />
<xs:element name="GetHotelMediaRS">
<xs:annotation>
<xs:documentation xml:lang="en">
Hotel Media Response Message provides hotel media content available for the specified hotel property code.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="stlp:STL_Response_Payload">
<xs:sequence>
<xs:element name="HotelMediaInfos" type="HotelMediaInfos">
<xs:annotation>
<xs:documentation xml:lang="en">
Contains information about one or more hotel codes and corresponding available media types.
This depends on the hotel codes provided in the request.
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="HotelMediaInfos">
<xs:annotation>
<xs:documentation xml:lang="en">
Contains information about one or more hotel codes and corresponding available media types.
This depends on the hotel codes provided in the request.
</xs:documentation>
</xs:annotation>
<xs:sequence minOccurs="0" maxOccurs="50">
<xs:element name="HotelMediaInfo" type="HotelMediaInfo">
<xs:annotation>
<xs:documentation xml:lang="en">
Contains infroamtion about various media types available for a particular hotel code.
This depends on the hotel code provided in the request.
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
You should simply remove the complexType definition HotelMediaInfos, and you would end up with something like the bellow xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:stlp="http://services.sabre.com/STL_Payload/v02_02" xmlns="http://services.sabre.com/hotel/media/v2" targetNamespace="http://services.sabre.com/hotel/media/v2" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://services.sabre.com/STL_Payload/v02_02" schemaLocation="built-ins/STL2_Payload_v02_02.xsd" />
<xs:include schemaLocation="HotelMediaCommons_v2.0.0.xsd" />
<xs:element name="GetHotelMediaRS">
<xs:annotation>
<xs:documentation xml:lang="en">
Hotel Media Response Message provides hotel media content available for the specified hotel property code.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="stlp:STL_Response_Payload">
<xs:sequence>
<xs:element name="HotelMediaInfos" type="HotelMediaInfos">
<xs:annotation>
<xs:documentation xml:lang="en">
Contains information about one or more hotel codes and corresponding available media types.
This depends on the hotel codes provided in the request.
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
You can even replace the content above in the GetHotelMediaRS_v2.0.0.xsd, and you should be able to compile.
Personal recommendation, in the future, try to do these things yourself. It will help you learn and avoid depending on Sabre or the community.

How to validate XSD schema in java? I am getting different ans when i hanle integer datatype

I want to validate AGE filed in my schema but i have given some restriction on it but when i give the string value in AGE filed which is not throwing the error like "Invalid data for AGE". May i know how to solve this issue? Please find my sample code below.
XML File
<?xml version="1.0"?>
<Employee>
<AGE>thf</AGE>
</Employee>
XSD File
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="AGE">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="65"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Error while generating JAXB model from xsd in eclipse

I am trying to generate JAXB model from the following xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://abc.com/mmm/trt" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:annotation>
<xs:documentation>Schema for Cache Refresh Event.
</xs:documentation>
</xs:annotation>
<xs:simpleType name="cacheCode">
<xs:restriction base="xs:string">
<xs:enumeration value="BUSINESS_RULE" />
<xs:enumeration value="USER" />
</xs:restriction>
</xs:simpleType>
<xs:element name="cacheRefreshEvent">
<xs:complexType>
<xs:sequence>
<xs:element name="cacheCode" type="cacheCode" minOccurs="1"
maxOccurs="1" />
<xs:element name="entityRefId" type="xs:string" minOccurs="0"
maxOccurs="20" />
<xs:element name="cacheRefreshDate" type="xs:dateTime"
minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But, it is giving me the following error, which i got in Eclipse console
parsing a schema...
[ERROR] src-resolve.4.1: Error resolving component 'cacheCode'. It was detected that 'cacheCode' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'. If 'cacheCode' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'cacheCode' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'.
line 18 of file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd
Failed to parse a schema.
Please help.
Remove this extra namespace attribute targetNamespace="http://www.hcentive.com/mea/trr"
OR add this xmlns="http://www.hcentive.com/mea/trr"
Both solutions will work :)
For you reference targetnamespace-and-xmlns

XSD Basic Help Needed -- Namespaces in Schemas?

I've got something like the following XML file.
<credits>
<property name="tag">
<item>v0003</item>
</property>
<property
name="tag">
<item>mhma03h</item>
</property>
</credits>
First requirement is that this XML cannot change no matter what. Please don't suggest doing that below.
I need to write a schema that validates this. And Java code that does the validation.
I am completely stuck and I have no idea really what is going on.
What's a schema like this look like? I've gotten one but it's so bad I'm not going to bother posting. :P I don't want to have to append a namespace to the XML elements. They're set in stone.^_^
How do I just make it so all of these elements are just "found" by the parser? Can I just tell it ignore all this namespace nonsense. With this application of validating against a schema, namespace conflicts are simply impossible.
I've tried putting
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="books" elementFormDefault="unqualified">
for my namespace info and
UPDATE: I've updated what I'm doing to reflect the answers given so far! :)
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:element ref="item"/>
</xs:sequence>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="mhma03h"/>
<xs:enumeration value="v0003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="credits">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML
v0003
Code to load and validate yes. Before you ask, the files are able to be loaded. I've checked like 20 times. :P
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
factory.setSchema(schemaFactory.newSchema(
new Source[] {new StreamSource("small.xsd")}));
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
org.xml.sax.XMLReader reader = parser.getXMLReader();
reader.setFeature("http://xml.org/sax/features/validation", true);
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
reader.parse(new InputSource("small.xml"));
Here is a schema that corresponds to the XML file you posted:
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2011 sp1 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:element ref="item"/>
</xs:sequence>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="mhma03h"/>
<xs:enumeration value="v0003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="credits">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This may not correctly capture the requirements for each element and attribute (check that the min/max occurs, required/optional proprerties, etc... are set correctly) but it should get you started on working with an XML schema that will validate correctly. The schema does not define a target namespace so you won't have to worry about modifying the existing XML to add in namespace prefixes to your existing elements.
You need to find a way to tell the XML processor to use your namespace as the default when it is processing a document without an explicit namespace. Most processors have a way to do this, IIRC there is a method named setNoNamespaceSchema or something similar. You would write an XML schema with a namespace and tell the processor (validator, whatever) to use your namespace for documents that do not have an explicit namespace.

Categories