Issue with generating sources from xsd schema - java

I'm getting the following error when generating sources for my project. I have extracted a few common types to a schema called CommonTypes.xsd and I get the following error:
org.xml.sax.SAXParseException: src-resolve.4.1: Error resolving component 'nonEmptyString'. It was detected that 'nonEmptyString' has no namespace, but components with no target namesp
ace are not referenceable from schema document 'file:/C:/Workspace/CommonTypes.xsd'. If 'nonEmptyString' is
intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'nonEmptyString' has no namespace, then an 'import' without a "namespace" attribute should
be added to 'file:/C:/Workspace/lps-performance-calculation-service/pcs-data/src/main/resources/xsd/calc/lps/CommonTypes.xsd'.
The following simple type is defined in my CommonTypes.xsd schema as below:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:unit="http://www.mywebsite.com/unit"
xmlns:types="http://www.mywebsite.com/types"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://www.mywebsite.com//types">
<!-- import types -->
<xsd:import namespace="http://www.mywebsite.com/unit"/>
<!-- other common types -->
<xsd:simpleType name="nonEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:pattern value=".*[^\s].*"/>
</xsd:restriction>
</xsd:simpleType>
And line 241 which causes the error is below:
<xsd:complexType name="Message">
<xsd:simpleContent>
<xsd:extension base="nonEmptyString">
<xsd:attribute type="xsd:string" name="code" use="required"/>
<xsd:attribute name="category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:token">
<xsd:enumeration value="Error"/>
<xsd:enumeration value="Info"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute type="xsd:string" name="component" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Do you have any idea what may cause the error? I've tried searching through StackOverflow and experimenting with the targetNamespace and xmlns, but with no success.

You are trying to refer to a simple type which has
name 'nonEmptyString'
namespace ""
But the simple type 'nonEmptyString' is defined in this XSD, which has targetNamespace="http://www.mywebsite.com//types". So you should be referring to a simple type which has
name 'nonEmptyString'
namespace "http://www.mywebsite.com//types"
You simply need to change this:
<xsd:extension base="nonEmptyString">
to this:
<xsd:extension base="types:nonEmptyString">

You need to import your nonEmptyString in the corresponding namespace and make this namespace referenceable via the prefix.
To do this, add xmlns:types="http://www.mywebsite.com/types" to xsd:schema of the importing schema.
Also provide the namespace in xsd:import of the importing schema. Should be something like:
<xsd:import
namespace="http://www.mywebsite.com/types"
schemaLocation="calc/lps/CommonTypes.xsd"/>
Then you should be able to reference your nonEmptyString type as types:nonEmptyString.

Related

How to generate case sensitive fields in java from wsdl using cxf-codegen-plugin?

I'm using cxf-codegen-plugin and try to generate java from wsdl.
In my xsd schema I have following element
<xsd:element name="SomeElement">
<xsd:complexType>
<xsd:attribute name="version" type="xsd:string" use="optional">
</xsd:attribute>
<xsd:attribute name="Version" type="xsd:string" use="optional">
</xsd:attribute>
</xsd:complexType>
</xsd:element>
CXF tries to generate both attributes into field version and throws exception that property already exists.
Is there any way to generate properties as they described in XML? e.g.
protected String version;
protected String Version;
I know, that I can define binding and rename one of the properties, but I have ~100 wsdl, so I don't want write bindings for each file.
I also tried to add -autoNameResolution argument, but it didn't help.

Character limit in webservice at wsdl level

I hace deployed a java WebService in an application using WebSphere AST. I need to limit the length of some fields (Strings) and I wondered if I can do that at the wsdl instead of coding some validations at java level.
I mean, if rigth now I have the elements defined like this:
<element name="code" nillable="true" type="xsd:string"/>
Can I set some property that restrict the max length of "code"?
Thank you.
<element name="code" nillable="true" type="xsd:string"/>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="2"/>
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>

Can the IDREF attribute have only local ID value?

I have such element
<xsd:element name="Car" type="carType"/>
<xsd:complexType name="carType">
<xsd:complexContent>
<xsd:extension base="basicType">
<xsd:attribute name="motor" type="xsd:IDREF" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
When motor element in the current document, it is work fine.
<Car id="car1" motor="motor1"/>
<Motor id="motor1"/>
But when I would like to import motor element from another file
<beans:bean:import resource="motors.conf.xml"/>
Intellij Idea say Invalid id reference, and when I run program I get an exception
There is no ID/IDREF binding for IDREF
May be I'm doing something wrong? Or may be xsd:IDREF equals ref local, and so I can't use it with import?
I was right, xsd:IDREF equals ref local.
About xsd:IDREF MSDN Creating Valid ID, IDREF...
And you can see why it equals here -
<xsd:element name="ref">
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines a reference to another bean in this factory or an external
factory (parent or included factory).
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:attribute name="bean" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the referenced bean.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
**<xsd:attribute name="local" type="xsd:IDREF">**
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the referenced bean. The value must be a bean ID and thus can
be checked by the XML parser. This is therefore the preferred technique
for referencing beans within the same bean factory XML file.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="parent" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the referenced bean in a parent factory.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
It is a description of element bean element ref. And as we know, we can use <ref local> only for elements in current XML document.

JAXB XJC - XPath evaluation results in empty target node?

I've got the following simple XSD document (foo.xsd):
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:foo">
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="Height">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Short"/>
<xsd:enumeration value="Average"/>
<xsd:enumeration value="Tall"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
And I'd like to hint to the XJC JAXB compiler that the "Height" element should use a type safe enum class by using an external bindings file, like so (foo.xjb):
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3c.org/2001/XMLSchema"
jxb:version="2.0">
<jxb:bindings schemaLocation="foo.xsd">
<jxb:bindings node="//xsd:element[#name='Height']/xsd:simpleType">
<jxb:typesafeEnumClass name="Height" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
But when I run the command "xjc -b foo.xjb foo.xsd" I get the following error:
parsing a schema...
[ERROR] XPath evaluation of "//xsd:element[#name='Height']/xsd:simpleType" results in empty target node
line 6 of file:/Users/maerics/src/java/jaxb/foo.xjb
Failed to parse a schema.
The XPath expression looks fine to me so I'm guessing there is some subtle problem related to XML namespaces? I've tried a few combinations of using (or not) a default namespace, targetNamespace, etc. but always the same error. Note that xjc generates Java source for the XSD file by itself, without the external bindings file, as expected. Similarly, using embedded binding definitions in the XSD file works as expected.
Note that I am using Java version "1.6.0_26" and xjc version "JAXB 2.1.10 in JDK 6" on Mac OS 10.6.8.
Can someone explain how to achieve this goal without modifying the original XSD?
Heh, you're going to kick yourself when you see the problem:
In foo.xsd, you have this:
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
In foo.xjb, you have this:
xmlns:xsd="http://www.w3c.org/2001/XMLSchema"
Note "w3" vs. "w3c". Those two attributes need to match exactly, and then your XPath will work (otherwise the namespace referenced in your xjb is distinct from the XSD namespace referenced in your XSD.)

How to create a generic XSD type

I have a java application where i can map a XSD type to another with same type. Now i have requirement to have one anyType xsd to which i can map any type. Like as we have Object type in java, is it possible to create like in XSD.
Edit: At complex type level is it possible.
Yes, it's possible. The type is xsd:anyType. Here's an example:
<xsd:element name="anything" type="xsd:anyType"/>
(Taken from the primer)
Here's a more complex example:
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value" type="xsd:decimal"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
(From the primer as well - it's worth looking at at it)
You could use the xs:any element - this allows you to have a section of you schema that can contain any arbitrary XML.

Categories