xjc: XML Validation issue - java

I am trying to generate java code using xjc.
My xsd is having following element
<xs:element name="DataRate">
<xs:attribute name="Limit" type="xs:decimal" use="optional" />
</xs:element>
and xjc is complaining with
[ERROR] s4s-elt-must-match.1: The content of 'DataRate' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: attribute.
After little investigation, i got to know that the attribute element is not wrapped with complexType and hence xjc is complaining. If I modify the xsd as
<xs:element name="DataRate">
<xs:complexType>
<xs:attribute name="Limit" type="xs:decimal" use="optional" />
</xs:complexType>
</xs:element>
it is working fine. But, as the xsd I am getting from 3rd party, I want to avoid the modification.
Is there any other way to do that?

Related

Can XSD Assertions be Used in Specifying a WSDL File

I have a WSDL file which also contains all types used in it (via <wsdl:types>
tag). When defining the types, I have something like this:
<wsdl:definitions name="service"
targetNamespace="http://www.xxx.yyy/reg/definitions" xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tax="http://www.xxx.yyy/reg/definitions" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
.............
<xs:complexType name="CompanyName">
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="ShortName" type="xs:string" minOccurs="0" />
</xs:sequence>
<xs:attribute name="Language" type="tax:LanguageType" use="required"/>
<xs:assert test="ShortName or #Language != 'Language1'"/>
</xs:complexType>
.............
</wsdl:definitions>
Unfortunately, it doesn't work giving the following exception when I try to start the application on Tomcat:
javax.xml.ws.WebServiceException: org.xml.sax.SAXParseException; s4s-elt-invalid-content.1: The content of 'CompanyName' is invalid. Element 'assert' is invalid, misplaced, or occurs too often.
The WSDL version is 1.2 and I don't know which version of xsd it uses when types are described in it, so I don't know if it is a xsd version (1.0 vs. 1.1) issue or something else.
Can someone help me in finding the real issue?
EDIT: I have added the header of the wsdl.
I have added the version attribute (version="1.1") to <xs:schema>
definition but that didn't help either:
<xs:schema targetNamespace="http://www.xxx.yyy/reg/definitions" elementFormDefault="qualified" version="1.1">
Looks like assertions were introduced with XML Schema v1.1 when the XMLSchema definition moved over to w3.org.
Sample header:
<wsdl:description
targetNamespace="http://www.w3.org/2002/ws/sawsdl/spec/wsdl/order#"
xmlns="http://www.w3.org/2002/ws/sawsdl/spec/wsdl/order#"
xmlns:wsdl="http://www.w3.org/ns/wsdl"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sawsdl="http://www.w3.org/ns/sawsdl">

JiBX generates binding that ignores attribute namespace

I have the following schema (fragment):
<xs:complexType name="partnerPaymentsItemType">
<xs:sequence>
<xs:element name="changeTime" type="dateTime"/>
<xs:element name="statusId" type="shortId"/>
<xs:element name="paymentPointId" type="shortString"/>
<xs:element name="money" type="currency"/>
<xs:element name="paymentDestination" type="shortString"/>
<xs:element name="paymentDestinationType" type="shortId"/>
<xs:element name="subagentId" type="shortId" minOccurs="0"/>
<xs:element name="discountCardNumber" type="xs:string" minOccurs="0"/>
<xs:element name="amountAll" type="currency" minOccurs="0"/>
<xs:element name="rewardPercent" type="percentAmount" minOccurs="0"/>
<xs:element name="rewardPercentValue" type="percentAmount" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="paymentTime" use="required" type="dateTime"/>
<xs:attribute name="externalId" use="required" type="id"/>
<xs:attribute name="registeredId" use="required" type="id"/>
</xs:complexType>
I use JibX Codegen tool to generate sources from it, and then to compile binding that should allow me to unmarshal XML to Java objects. Here is my codegen settings:
<schema-set xmlns:xs="http://www.w3.org/2001/XMLSchema"
delete-annotations="true"
prefer-inline="false"
generate-all="true"
show-schema="true"
type-substitutions="xs:date xs:string"
package="here.lays.my.package">
<class-decorator class="org.jibx.schema.codegen.extend.SerializableDecorator"/>
</schema-set>
Later, I try to parse an XML document which has namespace prefixes for both tags and attributes, which results an exception saying
Missing required attribute "paymentTime"
Debugging through JiBX sources shown that it tries to look for attribute with no namespace and name "paymentTime" in a document, while a document has an attribute with a namespace mapped to URL, and not able to find it of course.
I have decompiled binding with JAD and it searches for attribute with null namespace:
public static PartnerPaymentsItemType JiBX_beeline_binding_unmarshalAttr_1_93(PartnerPaymentsItemType arg1, UnmarshallingContext arg2)
throws JiBXException
{
arg2.pushTrackedObject(arg1);
arg1;
arg1.setPaymentTime(arg2.attributeText(null, "paymentTime"));
arg1.setExternalId(Utility.parseLong(WhitespaceConversions.trim(arg2.attributeText(null, "externalId"))));
arg1.setRegisteredId(Utility.parseLong(WhitespaceConversions.trim(arg2.attributeText(null, "registeredId"))));
arg2.popObject();
return arg1;
}
I would be grateful for any advise that could help resolving the issue - like, why JiBX generated such mapping, how to make it respect attribute namespace, and so on.
JiBX behavior was actually correct, namespace prefixes for attributes were not intended based on xsd.
Actual solution was to replace JiBX with XJC/JAXB and separate schema for the entity where I needed namespace prefixes for attributes.

How to maintain package structure when using schemagen and xjc

I am new to JAX B. I created a project with the structure :
src-
entity-
Person
property-
Address
I generate a xsd document using the command
C:\workspace\JAXTest\src>schemagen entity\Person.java property\Address.java
I see schema1.xsd in src folder now but it does not have any package information. So, If I use xjc to generate classes, I get all the classes in the same folder.
C:\workspace\JAXTest\src>xjc schema1.xsd
parsing a schema...
compiling a schema...
generated\Address.java
generated\ObjectFactory.java
generated\Person.java
What can be done so that the xsd can contain the package information so that the generated classes follow the structure of the classes used to create the xsd.
Thanks in advance!
Edit :
There is no particular requirement I am working towards. I am just trying to learn XSD-Class and Class-XSD conversions correctly. Also, I have found that I can use jax customization to specify the global package name but it doesn't work for individual classes. Basically, this does not put my Person in PersonPackage package.
<xs:complexType name="person">
<xs:annotation>
<xs:appinfo>
<jaxb:package name="PersonPackage"/>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="address" type="address" minOccurs="0">
</xs:element>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

How to retrieve values sent from webservice

I have a question about retrieving data values which are sent from a web service. I have a web service which receives data from a client and then does something with it. It worked all perfectly.
But now I wanted to add an extra element to the xsd which handles the message the webservice receives. I've added tests to another complexType which also exists of other elements. The element tests isn't required, but users can add one or more tests in their XML file.
<xs:element name="tests">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="999">
<xs:element name="test">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="code" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Whole XSD: http://pastebin.com/TuvYiQzE
I am using JAXB to handle the webservice messages which are send from the client. JAXB automatically generated some code for me:
public Message.Algemeen.TestCode.Tests getTests(){
return tests;
}
And
public List<Message.Algemeen.TestCode.Tests.test> getTest() {
if (test== null) {
test= new ArrayList<Message.Algemeen.TestCode.Tests.test>();
}
return this.test;
}
Now I want to return code and value per element test, the problem is Test and tests are a returned as an object and at the moment I have no idea how to read their value. The method toString() just returns cp.jaxb.classes.Message$Algemeen$Testcode$Tests$test#dcd76a
What am I doing wrong? If you need more code to understand my problem please tell me.
Thanks,
Jef
PS. English isn't my native language, I tried my best to explain my problem.
This line <xs:sequence minOccurs="1" maxOccurs="unbounded"> in the definition of Test means that you can have several code:value pair in test. is this what you want?
I'm no jaxb expert but I was told when designing schemas that it's easier to used name types in this case.
Here you have an list of "unnamed" object (the code:value pair) in a test. And I guess that makes the retrieval difficult.
What happen if you changed to maxOccurs="1"?
what Happen if you defined a new type for your code:value pair and make a list of this element?
could you try to modify it like this:
<xs:element name="tests">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="999">
<xs:element name="test">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="singleTest">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="code" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
then you should be able to have something like
List testList = test.getSingleTest()
and iterate to get the code and value for each of them.
What language are you writing your client in? If you're using Java or .Net for example, you should simply be able to invoke your web service method and get a fully formed instance of Test back. As Udo Klimaschewski points out above, this means that you should be able to use something like
getTest().getCode()
To be clear, while it's potentially interesting to look through the XSD, you should not actually need to do this unless your development environment lacks SOAP web service support. The exact mechanism for generating the client side artifacts depends on your language and development environment; as an example, the process for referencing a web service using Netbeans is described here:
http://netbeans.org/kb/docs/websvc/client.html
This should work:
List<Message.Algemeen.TestCode.Tests.test> tests = yourObject.getTest();
for (Message.Algemeen.TestCode.Tests.test test : tests) {
test.getCode(); //Here is test object which contains strings or whatever.
}

Xml schema interfaces

I'm attempting to write an XML schema to define some model classes to be used in jaxb in java.
I would like to be able to define an interface in the xsd but I've no idea how to do this.
For example a lot of my classes will have an attribute called id which is type xs:ID. They will have an attribute called ref which is type xs:IDREF and they will have an attribute called extRef which means that there is a refrence but the element is not specified in the given xml file.
Ideally this would be an interface when converted to java for two reasons.
The classes that implement this are not necessarily connected.
I would like to specify a different inheritance tree for some of these objects.
Example:
<!-- Utility type to be externally referenceable this is too keep all the
externally refrencable attributes in check -->
<xs:complexType name="ExternallyReferenceable" abstract="true">
<xs:attribute name="extRef" type="externalReference" />
<xs:attribute name="id" type="xs:ID" />
<xs:attribute name="ref" type="xs:IDREF" />
</xs:complexType>
<xs:complexType name="ExternalCarPart">
<xs:extension base ="ExternallyReferenceable" />
</xs:complexType>
<xs:complexType name="CarPart">
<xs:extension base ="ExternalCarPart">
<xs:sequence>
<xs:element type="xs:string" name="partName" />
</xs:sequence>
</xs:extension>
</xs:complexType>
<xs:complexType name="CarLamp">
<xs:extension base ="ExternalCarPart">
<xs:sequence>
<xs:element type="xs:string" name="glassPurity" />
</xs:sequence>
</xs:extension>
</xs:complexType>
So in the above example it would be better if exteranllyRefrencable was an interface, and if carpart was an interface.
This would allow CarLamp to inherit from lamp rather than external car part. BicycleLamp could also implement the externallyRefrencable interface and extend lamp also.
Please ask me for clarification if the question isn't clear.
IIRC, JAXB doesn't generate interfaces from XSD.
JAXB also supports generating XSD from Java classes. I'm not sure what it does with interfaces in that regard, but perhaps you need to go at it in the opposite direction - begin with Java and generate XSD, though I'm not sure that will help you.

Categories

Resources