Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1015
  • Last Modified:

WSDL operations: input and output complex types : attribute vs. element

Hi there,

this is a webservice / wsdl question.

when defining the input and output complex types in a wsdl Operation, we can use elements or attributes for those complex types.

what is the point with choosing attributes instead of elements ?

As i can see, the classes used make no difference:
if i define in wsdl an operation findBooksByCriteria,
with an input complex type Criteria containing elements maxPrice and minPrice
and
with an output complex type booksList containing Books with attributes Title, Author, Price

i can use a class Criteria with properties maxPrice and minPrice as a parameter when calling the method on soapClient,

and i get an object containing a list of objects with properties Title, Author and Price.

Everything works fine (php 5.3), but i don't understand when / why use attribute or element.

i understand the difference between an element and an attribute (in XSD point of view) but i can't see the implications in the corresponding classes.

Thank you for your time and help.
0
Gaston66
Asked:
Gaston66
  • 2
  • 2
1 Solution
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
This is a very generic and conceptual question, it only depends on the way you want to design your service, let's say I was designing a EmployeeWebService, where Employee is the main resource/class, I would prefer something which uniquely identifies a employee "empId" (which acts like a key) to be attribute on the employee which is easier to query using Xpath. but nothing is stopping me from making empId an element. it's only depends on the way you want to design it to be, most people avoid attributes as some tools/programs are not very efficient at handling them.

A nice article about it is here : http://www.ibm.com/developerworks/xml/library/x-eleatt/index.html

simple rule:

   1. An Attribute is something that is self-contained, i.e., a color, an ID, a name.
   2. An Element is something that does or could have attributes of its own or contain other elements.

General problems with attributes are:

    * attributes cannot contain multiple values (child elements can)
    * attributes cannot describe structures (child elements can)
    * attributes are more difficult to manipulate by program code
0
 
Gaston66Author Commented:
Hello, thank you for your time.

As i said in my question, i understand the difference between an element and an attribute (in XSD point of view).

My problem is the "O" in sOap.

In a contract-first approach, having thus defined my WSDL with the included types (in the xsd part of it), next step is to buil the classes, both on server and client side.

From what i know, i have to build classes with corresponding properties, and use those classes as input values or output values for the exposed operations.

So, my operation returns a Book, with attribute ISSN and elements Authors (1..n), my server has to return an object Book with properties (String)ISSN and (Array)Author.

My question is : from that Class point of view, there is no way (and no need?) to make a difference between what is supposed to map the attribute (ISSN) and the element (Author), both will be object's propoerties at the end. Or did i miss something ?
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
Sorry for the delay in responding.

from that Class point of view, there is no way (and no need?) to make a difference between what is supposed to map the attribute (ISSN) and the element (Author), both will be object's propoerties at the end.

As the classes are being generated from the WSDL your web service engine depending on the technology you are using (if java it can be JAX-WS or AXIS2 etc if .NET then it's the either WCF or .NET Framework itself)  the mapping b/w the xml element and attribute are maintained.

From Class perspective both attributes and elements are just fields inside a Class :) it's kind of flatted out as OOP and Document Model are totally different :)
0
 
Gaston66Author Commented:
still frustrated by the lack of justification for complexifying the wsdl if it is flattened at the end object...
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now