In XML schema, the wildcard <xsd:any> element allows the
complex types to be extended with elements not specified by the schema.
This <xsd:any> element can also be useful when the contents of
the
complex type don’t actually need to be defined in the schema. For
example, consider a situation where the service operation may receive
more than one type of purchase order XML document, and each purchase
order has a different schema defined. In this case, it's useful to
describe the element purchase order document type as <any> in the
service interface. This makes the endpoint more generic. It also means
that the service implementation code must determine which type of xml
document it receives and handle it appropriately. Using <any>
will make the service operation more generic, so your code must be able
to handle it. In the WSDL file in Code
Example 1, note that there is a type submitPO defined which
includes an <any> element, so it sort of wraps the <any>
inside another element declaration. This submitPO type is then used in
the service interface for the operation to submit a purchase order.
(Note that the element name submitPO and the operation name submitPO
are
the same, and this is just because the WSDL uses the wrapped style
which specifies a convention of these names matching.) For this
service,
the document being received can have any elements in the XML.
<complexType name="submitPO">
<sequence>
<any/>
</sequence>
</complexType>
<element name="submitPO" type="tns:submitPO"/>
</schema>
</types>
<message
name="AnyPurchaseOrderServiceSEI_submitPO">
<part name="parameters" element="tns:submitPO"/>
</message>
...
<portType
name="AnyPurchaseOrderServiceSEI">
<operation name="submitPO">
<input
message="tns:AnyPurchaseOrderServiceSEI_submitPO"/>
...
</operation>
</portType>
Code Example 1: WSDL Snippet Showing
the Usage of <any> for a Service Operation Input Parameter
In JAX-RPC when the xsd:any schema type element is used to represent
element wildcards, the mapping of the complex type will get mapped to a
JavaBeans[TM] component. The presence of the <any> element with a
maxOccurs=1 results in an additional property called _any which maps to
a javax.xml.soap.SOAPElement (if the maxOccurs is greater than 1, it
maps to an array of javax.xml.soap.SOAPElement). Also, in JAX-RPC, if
there is no standard Java mapping for an XML schema type, a message
part
with literal representation is considered and mapped as an XML document
fragment. The XML to Java mapping uses the interface
javax.xml.soap.SOAPElement to represent a literal message part in the
Java mapping of a wsdl:operation element. As a result, if you generate
a
Java interface from a WSDL file such as in Code
Example 1 which uses <any> for the document being received,
then your Java interface may have a wrapper class of type SubmitPO, as
shown in Code Example 2. In this case,
the Java implementation of this interface will need to call a getter
method get_any()
on the SubmitPO parameter to get
the
SOAPElement which is wrapped inside. This extra step to call is a bit
inconvenient but not much trouble.
public interface
AnyPurchaseOrderServiceSEI extends Remote {
public SubmitPOResponse submitPO(SubmitPO
parameters) throws
InvalidPOException,
java.rmi.RemoteException;
}