[web-services] Importing xsd into wsdl

This is my current configuration:

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://stock.com/schemas/services/stock"
    xmlns:tns="http://stock.com/schemas/services/stock"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"  targetNamespace="http://stock.com/schemas/services/stock">

<xsd:element name="Stock">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="ticker" nillable="true" type="xsd:string"/>
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="quotes" nillable="true" type="Quote"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:complexType name="Quote">
    ........
</xsd:complexType>
.......
<xsd:element name="gethighBetaStockResponse">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="stock" ref="Stock" minOccurs="1" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

WSDL

<?xml version="1.0" encoding="UTF-8"?><definitions targetNamespace="http://stock.com/schemas/services/stock/wsdl"
    .....xmlns:external="http://stock.com/schemas/services/stock"
<import namespace="http://stock.com/schemas/services/stock" location="Stock.xsd" />
<message name="getStockQuoteResp">
    <part name="parameters" element="external:getStockQuoteResponse" />
</message>

However,the moment ref="Stock" is changed to type="Stock",the wsdl2java starts giving Type {http://stock.com/schemas/services/stock}Stock is referenced but not defined.

Somehow it seems a clash between wsdl and xsd imports - but I just cant resolve it.Help is appreciated.

This question is related to web-services xsd wsdl wsdl2java

The answer is


You have a couple of problems here.

First, the XSD has an issue where an element is both named or referenced; in your case should be referenced.

Change:

<xsd:element name="stock" ref="Stock" minOccurs="1" maxOccurs="unbounded"/> 

To:

<xsd:element name="stock" type="Stock" minOccurs="1" maxOccurs="unbounded"/> 

And:

  • Remove the declaration of the global element Stock
  • Create a complex type declaration for a type named Stock

So:

<xsd:element name="Stock">
    <xsd:complexType>

To:

<xsd:complexType name="Stock">

Make sure you fix the xml closing tags.

The second problem is that the correct way to reference an external XSD is to use XSD schema with import/include within a wsdl:types element. wsdl:import is reserved to referencing other WSDL files. More information is available by going through the WS-I specification, section WSDL and Schema Import. Based on WS-I, your case would be:

INCORRECT: (the way you showed it)

<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://stock.com/schemas/services/stock/wsdl"
    .....xmlns:external="http://stock.com/schemas/services/stock"
    <import namespace="http://stock.com/schemas/services/stock" location="Stock.xsd" />
    <message name="getStockQuoteResp">
        <part name="parameters" element="external:getStockQuoteResponse" />
    </message>
</definitions>

CORRECT:

<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://stock.com/schemas/services/stock/wsdl"
    .....xmlns:external="http://stock.com/schemas/services/stock"
    <types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema">
            <import namespace="http://stock.com/schemas/services/stock" schemaLocation="Stock.xsd" />             
        </schema>
    </types>
    <message name="getStockQuoteResp">
        <part name="parameters" element="external:getStockQuoteResponse" />
    </message>
</definitions>

SOME processors may support both syntaxes. The XSD you put out shows issues, make sure you first validate the XSD.

It would be better if you go the WS-I way when it comes to WSDL authoring.

Other issues may be related to the use of relative vs. absolute URIs in locating external content.


import vs. include

The primary purpose of an import is to import a namespace. A more common use of the XSD import statement is to import a namespace which appears in another file. You might be gathering the namespace information from the file, but don't forget that it's the namespace that you're importing, not the file (don't confuse an import statement with an include statement).

Another area of confusion is how to specify the location or path of the included .xsd file: An XSD import statement has an optional attribute named schemaLocation but it is not necessary if the namespace of the import statement is at the same location (in the same file) as the import statement itself.

When you do chose to use an external .xsd file for your WSDL, the schemaLocation attribute becomes necessary. Be very sure that the namespace you use in the import statement is the same as the targetNamespace of the schema you are importing. That is, all 3 occurrences must be identical:

WSDL:

xs:import namespace="urn:listing3" schemaLocation="listing3.xsd"/>

XSD:

<xsd:schema targetNamespace="urn:listing3"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

Another approach to letting know the WSDL about the XSD is through Maven's pom.xml:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>xmlbeans-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-sources-xmlbeans</id>
      <phase>generate-sources</phase>
      <goals>
    <goal>xmlbeans</goal>
      </goals>
    </execution>
  </executions>
  <version>2.3.3</version>
  <inherited>true</inherited>
  <configuration>
    <schemaDirectory>${basedir}/src/main/xsd</schemaDirectory>
  </configuration>
</plugin>

You can read more on this in this great IBM article. It has typos such as xsd:import instead of xs:import but otherwise it's fine.


Examples related to web-services

How do I POST XML data to a webservice with Postman? How to send json data in POST request using C# org.springframework.web.client.HttpClientErrorException: 400 Bad Request How to call a REST web service API from JavaScript? The request was rejected because no multipart boundary was found in springboot Generating Request/Response XML from a WSDL How to send a POST request using volley with string body? How to send post request to the below post method using postman rest client How to pass a JSON array as a parameter in URL Postman Chrome: What is the difference between form-data, x-www-form-urlencoded and raw

Examples related to xsd

How to generate xsd from wsdl How to reference a local XML Schema file correctly? XML Schema Validation : Cannot find the declaration of element Using Notepad++ to validate XML against an XSD Error in spring application context schema cvc-elt.1: Cannot find the declaration of element 'MyElement' Importing xsd into wsdl SoapUI "failed to load url" error when loading WSDL How to make an element in XML schema optional? XML Schema How to Restrict Attribute by Enumeration

Examples related to wsdl

Generating Request/Response XML from a WSDL How to generate xsd from wsdl How do you convert WSDLs to Java classes using Eclipse? How to get the wsdl file from a webservice's URL JSON, REST, SOAP, WSDL, and SOA: How do they all link together Difference between a SOAP message and a WSDL? SOAP PHP fault parsing WSDL: failed to load external entity? How to do a SOAP wsdl web services call from the command line Importing xsd into wsdl SoapUI "failed to load url" error when loading WSDL

Examples related to wsdl2java

Importing xsd into wsdl How to avoid the need to specify the WSDL location in a CXF or JAX-WS generated webservice client? Java Webservice Client (Best way) Access restriction on class due to restriction on required library rt.jar?