Importing xsd into wsdl

122,920

Solution 1

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.

Solution 2

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.

Share:
122,920
IUnknown
Author by

IUnknown

Updated on February 07, 2020

Comments

  • IUnknown
    IUnknown over 4 years

    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.

  • IUnknown
    IUnknown over 11 years
    I need to get the following form to work <xsd:element name="stock" type="Stock" minOccurs="1" maxOccurs="unbounded"/> Importing both into xsd and wsdl doesnt help <import namespace="stock.com/schemas/services/stock" location="Stock.xsd" /> <types> <schema xmlns="w3.org/2001/XMLSchema" targetNamespace="stock.com/schemas/services/stock/wsdl"> <import namespace="stock.com/schemas/services/stock" schemaLocation="Stock.xsd"/> </schema> </types>
  • IUnknown
    IUnknown over 11 years
    Silly mistake - but appreciate the help in identifying the discrepancy.