I have a problem generating equals and hashcode methods for my imported xsd files which are output to a separate location to my wsdl files. I currently have a .wsdl and .xsd file in the same folder. The .wsdl file imports these xsd files which have a different namespace to the wsdl file. When these xsd files are generated they do not contain equals or hashcode methods. Below is a sample of my pom/xml config and files:
<wsdlOption>
<wsdl>
src/main/xml/wsdl/Rule.wsdl
</wsdl>
<wsdlLocation>classpath:wsdl/Rule.wsdl</wsdlLocation>
<bindingFiles>
<bindingFile>src/main/xml/wsdl/bindings.xjb</bindingFile>
</bindingFiles>
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-verbose</extraarg>
<extraarg>-xjc-XsimpleEquals</extraarg>
<extraarg>-xjc-XsimpleHashCode</extraarg>
</extraargs>
</wsdlOption>
Sample wsdl (imported Rule.xsd is not generating methods):
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.ws.com/Rule/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Rule"
targetNamespace="http://www.ws.com/Rule/">
<wsdl:types>
<xsd:schema targetNamespace="http://http://www.ws.com/Rule/">
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ws.com/Rule/"
xmlns:pref="http://www.xsd.com/Rule" xmlns:pref1="http://www.xsd.com/Common">
<xsd:import schemaLocation="Rule.xsd"
namespace="http://www.xsd.com/Rule">
</xsd:import>
<xsd:import schemaLocation="Common.xsd" namespace="http://www.xsd.com/Common"></xsd:import>
<xsd:element name="ListGrid">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="request"
type="pref:test" maxOccurs="1" minOccurs="1">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
I assume this is a problem with the location of the generated xsd as I can get it to work by moving this to the same parent folder as the wsdl namespace. I would like to get this to work without moving these files locations as I have a large codebase that would require a large refactor if I do this.
Any suggestions would be welcome.
I found a solution to this problem by adding the following to the pom.xml configuration:
<extraarg>-p</extraarg>
<extraarg>http://www.xsd.com/Rule=com.xsd.rule</extraarg>
Even though this does not change the namespace/package location it seems to now generate the imported files equals and hashcode methods.
Related
I'm having the following "imp.xsd":
<xsd:schema targetNamespace="http://imported"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Imported" type="xsd:string"/>
</xsd:schema>
imported from "incl.xsd":
<xsd:schema targetNamespace="http://main"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://imported" schemaLocation="file:///C:/.../imp.xsd"/>
<xsd:element name="Included" type="xsd:string"/>
</xsd:schema>
which in turn is included from "main.xsd":
<xsd:schema targetNamespace="http://main"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="file:///C:/.../incl.xsd"/>
<xsd:complexType name="dummy">
<xsd:sequence xmlns:impt="http://imported" xmlns:incl="http://main">
<xsd:element ref="incl:Included"/>
<xsd:element ref="impt:Imported"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Question: is "main.xsd" a valid xml schema document?
When validated with the xmllint parser (or from python lxml library, which in turn uses xmllint), I get the following error:
Element '{http://www.w3.org/2001/XMLSchema}element', attribute 'ref':
References from this schema to components in the namespace
'http://imported' are not allowed, since not indicated by an import
statement. WXS schema main.xsd failed to compile
However, another (IBM) parser accepts this XSD without any complaints.
As far as I understand xsd:include, it should behave as if the whole included XSD is inlined into the main document. So, in my opinion, the "main.xsd" should be valid. Is it?
Edit: I was experimenting with xmllint a bit, and when I add the line
<xsd:import namespace="http://imported" schemaLocation="file:///C:/.../imp.xsd"/>
into "main.xsd", xmllint accepts that schema. However, when I add the same line while omitting file:///, I get the following warning:
Element '{http://www.w3.org/2001/XMLSchema}import': Skipping import of
schema located at 'C:/.../imported.xsd' for the namespace
'http://imported', since this namespace was already imported with the
schema located at 'file:///V:/.../imported.xsd'.
Hence I deduce that the original "main.xsd" is valid and that this is a bug in xmllint.
See this thread on the xmlschema-dev#w3.org mailing list. In his answer, Henry S. Thompson quotes following part of the spec:
For a ·QName· to resolve to a schema component [...] the
·namespace name· of the ·QName· is the same as one of the following:
[...]
The ·actual value· of the namespace [attribute] of some <import>
element information item contained in the <schema> element
information item of that schema document [emphasis added].
So indirect imports are not supposed to work.
I have a large .xsd file structured like this:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="foo:bar:baz" xmlns:quux="foo:bar:baz"
quux:attr1="A" quux:attr2="5">
<xsd:attribute name="attr1" type="xsd:string"/>
<xsd:attribute name="attr2" type="xsd:int"/>
<xsd:annotation>
<xsd:documentation>
<xhtml:h1 quux:attr1="A" quux:attr2="5">
Documentation here
</xhtml:h1>
</xsd:documentation>
</xsd:annotation>
<xsd:complexType name=... />
</xsd:schema>
I get the error: "The foo:bar:baz:attr1" attribute is not declared.
Why is it not finding the attribute? It's right there. How can I make these attributes available to the documentation's header?
Future edit: the above schema got the green light from actual XML Validators. Guess there was just something Visual Studio was reading incorrectly.
The XSD you show can be imported or included into another XSD that includes an element declaration, but alone it cannot be used to validate an XML file because it does not declare even a single element.
I got a wsdl file that defines two schemas for the same namespace, like this:
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com/payments"
attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://example.com/payments">
<xsd:complexType name="TestType">
<xsd:sequence>
<xsd:element name="Version" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com/payments"
attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://example.com/payments">
<xsd:element name="TestRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Header" type="TestType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
Sure enough TestType can't be referenced from the TestRequest. I've found several answers that show how you'd do that for different files via a xsd:include (as I'm using the same targetNamespace), but I'm at a loss at how to give the schemaLocation to include?
Note that whether not this is something I should be doing is not the question. I know the issue disappears if I move the element definitions into the same schema definition.
For XSDs embedded inside WSDLs, things are different than what you've looked at, considering the answer you've linked in your question.
Short answers:
one cannot create xsd:include references between schemas nested within a WSDL's types section.
xsd:import is supported by most WSDL processors. However, you do not supply the schemaLocation attribute. In effect, it creates a dangling schema reference, with the WSDL itself acting similarly to an XML catalog.
There is no standard way to point at schemas inside arbitrary XML files (other then the .XSD file format, where the whole file is for one schema only). Of course, XPointer or something similar might've worked; but nobody seems to have implement a solution for this in an XSD or WSDL processor.
The fact that your schemas with the same namespace are not working shows a limitation in the implementation of your WSDL processor. There should be no reason to have dangling imports resolved, but not schemas with a target namespace. The reason of the latter highlight is that schemas with no namespace can be included and/or imported, which would cause ambiguity.
WSDL is itself not the XSD Schema language, so to reference it from your XSD file through xs:include you will have to get rid of the surrounding wsdl:types and you will have to store both schema's in separate files (or, since they are in the same namespace anyway, simply remove the extra xsd:schema element, but not its contents).
You could do that with a trivial XSLT (root note removed for brevity, make sure to declare the relevant namespaces):
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<xsl:template match="wsdl:types">
<xsl:apply-templates select="xsd:schema[1]" />
</xsl:template>
<xsl:template match="xsd:schema">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
<xsl:apply-templates select="following-sibling::xsd:schema/*" />
</xsl:copy>
</xsl:template>
If this is a one-off task, then you can simply run this once against your WSDL, save the results and reference them by relative file URI. If you need to do this more often, you should probably automate this.
I have two xsd files. 1st file is common.xsd and the other is node.xsd. Both node.xsd and common.xsd share the same targetNamespace. common.xsd references an element defined in node.xsd using ref attribute. However, node.xsd is NOT included in common.xsd either using include or import. But the XML that I validate using these xsd files, passes the validation (Tried all corner usecases).
I wonder how this is possible. Is this because, they share the same namespace? Also is referencing an element without including/importing legal in XSD?
EDIT:
Simplified Code Snippets(The actual xsd's are much more complex and they are written in this format for bigger reason):
common.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:my="my-namespace"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
targetNamespace="my-namespace"
elementFormDefault="qualified">
<xsd:element name="common" type="my:commonType" />
<xsd:complexType name="commonType">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="my:node"/>
<!-- few other elements -->
</xsd:choice>
</xsd:complexType>
</xsd:schema>
node.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:my="my-namespace"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
targetNamespace="my-namespace"
elementFormDefault="qualified">
<xsd:include schemaLocation=common.xsd"/>
<xsd:element name="node" type="my:nodeType"
substitutionGroup="my:common" />
<xsd:complexType name="nodeType">
<xsd:complexContent>
<xsd:extension base="my:commonType">
<!-- some 5-7 attributes -->
<xsd:anyAttribute/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
These xsd's let me nest element within itself any number of times.
E.g
<my:node>
<my:node />
<my:node>
<my:node />
</my:node>
</my:node>
You can observe that my:node is referenced in common.xsd without including node.xsd. (Curious as to how this even works.)
I can make this look even more wicked... You can remove the xsd:include in node.xsd and still validate your XML! Take a look at this Xerces API for how you could do it.
The idea is that from a spec perspective, an XML Schema processor can resolve schema locations in many ways. It also means that some XSD files when looked at individually may not be valid due to dangling references, yet when put together through APIs like the one above, or custom resolvers (e.g. supporting "catalog" files) the result is an equivalent schema that is valid.
The way an XSD processor typically works, is that it puts together all the schema components that can be loaded through the references it can resolve, then it looks at the result as a whole, irrespective of where these components come from. In your case, node.xsd brings in common.xsd; the result is a valid schema, since all that is needed for components in common.xsd can be found among components already brought in by node.xsd.
In your case it is as if the inner content of the xsd:schema tag in common.xsd replaces the xsd:include in node.xsd. If you do that by hand, the result is correct, right?
As I side note, I would point out that the snippets you've shown don't illustrate the use of the common substitution group. As a reminder, you have to reference the head of the substitution group if you want you to get substitution going.
Does JAXB support modular code generation?
Most of my background is with JibX for XML marshalling, but for legacy reasons our firm is using JAXB.
One feature that was available for JIBX was modular code generation. Say I have a main schema but I have several different envelopes for that schema. With JibX I could create a jar file out of the JibX'ed core schema, and then in separate projects I could JibX my envelope schemas and simply point to the shared jar instead of having to duplicate the code generation of the core schemas for each envelope.
I don't yet see a way for JAXB to handle this - has anyone been successful doing something like this?
Thanks in advance,
Roy
For the JAXB RI, that's handled with "episode" files (these are really just customization files). Process the core schema first, making sure to have xjc use the -episode <file> arg. Package the results of that processing into a JAR file with the episode file in META-INF/sun-jaxb.episode. Then, pass that JAR file as an arg to xjc when processing the other schemas.
Using a JAXB 2.1 implementation (Metro, EclipseLink MOXy, Apache JaxMe, etc), you can specify that schema types correspond to existing classes in order to prevent them from being generated.
For example:
root.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/root">
<xsd:import schemaLocation="imported.xsd" namespace="http://www.example.com/imported"/>
<xsd:complexType name="root">
<xsd:attribute name="root-prop" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
imported.xsd
<?xml version="1.0"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/imported"
targetNamespace="http://www.example.com/imported">
<xsd:complexType name="imported">
<xsd:attribute name="imported-prop" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
Problem Statement
If you use the XJC tool to generate java classes from the XML schema:
xjc -d out root.xsd
You the following is generated:
com\example\imported\Imported.java
com\example\imported\ObjectFactory.java
com\example\imported\package-info.java
com\example\root\ObjectFactory.java
com\example\root\Root.java
com\example\root\package-info.java
imported-bindings.xml
You can use a JAXB bindings file to specify that types from imported.xsd point to existing classes:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="imported.xsd">
<jxb:bindings node="//xs:complexType[#name='imported']">
<jxb:class ref="com.example.imported.Imported"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Running the XJC
Now if we run XJC with out bindings file:
xjc -d out -b imported-bindings.xml root.xsd
None of the files specified in the bindings file will be generated:
com\example\root\ObjectFactory.java
com\example\root\Root.java
com\example\root\package-info.java
Alternative Approach
The code generated from the imported schema directly (xjc imported.xsd) and indirectly (xjc root.xsd) is the same. You can simply drop the code generated indirectly and point at the project containing the code that was generated directly.