gSOAP with OPC XML-DA add attribute
Question
I'm trying to develop a soap client that will access data over the OPC XML-DA specification: Here
I've used the tools provided by gSOAP to generate the gSOAP header file from the OPC Foundations WSDL. (Relevant parts below)
I can't seem to get gSOAP to properly add an attribute to the tag. (See Output section below). Is there a builtin way to do this, or will the WSDL/gSOAP header need to be modified?
WSDL extract:
<s:complexType name="ItemValue">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="DiagnosticInfo" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Value" />
<s:element minOccurs="0" maxOccurs="1" name="Quality" type="s0:OPCQuality" />
</s:sequence>
<s:attribute name="ValueTypeQualifier" type="s:QName" />
<s:attribute name="ItemPath" type="s:string" />
<s:attribute name="ItemName" type="s:string" />
<s:attribute name="ClientItemHandle" type="s:string" />
<s:attribute name="Timestamp" type="s:dateTime" />
<s:attribute name="ResultID" type="s:QName" />
</s:complexType>
Generated gSOAP header
class ns1__ItemValue
{ public:
/// Element DiagnosticInfo of type xs:string.
char* DiagnosticInfo 0; ///< Optional element.
/// Element 'Value' has no type or ref: assuming XML content.
_XML Value 0; ///< Optional element.
/// Element Quality of type "http://opcfoundation.org/webservices/XMLDA/1.0/":OPCQuality.
ns1__OPCQuality* Quality 0; ///< Optional element.
/// Attribute ValueTypeQualifier of type xs:QName.
@_QName ValueTypeQualifier 0; ///< Optional attribute.
/// Attribute ItemPath of type xs:string.
@char* ItemPath 0; ///< Optional attribute.
/// Attribute ItemName of type xs:string.
@char* ItemName 0; ///< Optional attribute.
/// Attribute ClientItemHandle of type xs:string.
@char* ClientItemHandle 0; ///< Optional attribute.
/// Attribute Timestamp of type xs:dateTime.
@time_t* Timestamp 0; ///< Optional attribute.
/// Attribute ResultID of type xs:QName.
@_QName ResultID 0; ///< Optional attribute.
/// A handle to the soap struct that manages this instance (automatically set)
struct soap *soap ;
};
Generated Code
class SOAP_CMAC ns1__ItemValue
{
public:
char *DiagnosticInfo; /* optional element of type xsd:string */
char *Value; /* optional element of type xsd:anyType */
class ns1__OPCQuality *Quality; /* optional element of type ns1:OPCQuality */
char *ValueTypeQualifier; /* optional attribute */
char *ItemPath; /* optional attribute */
char *ItemName; /* optional attribute */
char *ClientItemHandle; /* optional attribute */
time_t *Timestamp; /* optional attribute */
char *ResultID; /* optional attribute */
struct soap *soap; /* transient */
public:
virtual int soap_type() const { return 18; } /* = unique id SOAP_TYPE_ns1__ItemValue */
virtual void soap_default(struct soap*);
virtual void soap_serialize(struct soap*) const;
virtual int soap_put(struct soap*, const char*, const char*) const;
virtual int soap_out(struct soap*, const char*, int, const char*) const;
virtual void *soap_get(struct soap*, const char*, const char*);
virtual void *soap_in(struct soap*, const char*, const char*);
ns1__ItemValue() : DiagnosticInfo(NULL), Value(NULL), Quality(NULL), ValueTypeQualifier(NULL), ItemPath(NULL), ItemName(NULL), ClientItemHandle(NULL), Timestamp(NULL), ResultID(NULL), soap(NULL) { }
virtual ~ns1__ItemValue() { }
};
Output
<ns1:Items
ClientItemHandle="Channel1.Device1"
ItemName="Channel_1.Device_1.Tag_1"
ValueTypeQualifier="xsd:unsignedInt">
<Value
xmlns="http://opcfoundation.org/webservices/XMLDA/1.0/">
5
</Value>
</ns1:Items>
Needed Output
<ns1:Items
ClientItemHandle="Channel1.Device1"
ItemName="Channel_1.Device_1.Tag_1"
ValueTypeQualifier="xsd:unsignedInt">
<Value
xmlns="http://opcfoundation.org/webservices/XMLDA/1.0/"
xsi:Type="xsd:unsignedInt">
5
</Value>
</ns1:Items>
Output means the XML that is generated and sent over the wire to the remote server.
La solution 2
@Tisho, thanks for your input.
It turns out that the WSDL provided by the OPC Foundation lacked to specify a type on the value element. Our solution was to add a type of s:anyType, this allowed us to use polymorphic types such as:
xsd_unsignedInt
xsd_string
xsd__anyType
Since all of the types inherited from xsd__anyType, and a virtual soap_type function was included, we can use any of the types, and store them in the value, then gSOAP magically uses the soap_type to figure out what type the var is.
Relevant portion of the OPC Foundation's WSDL Modified:
<s:complexType name="ItemValue">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="DiagnosticInfo" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Value" type="s:anyType"/> <!-- Here -->
<s:element minOccurs="0" maxOccurs="1" name="Quality" type="s0:OPCQuality"/>
</s:sequence>
<s:attribute name="ValueTypeQualifier" type="s:QName"/>
<s:attribute name="ItemPath" type="s:string"/>
<s:attribute name="ItemName" type="s:string"/>
<s:attribute name="ClientItemHandle" type="s:string"/>
<s:attribute name="Timestamp" type="s:dateTime"/>
<s:attribute name="ResultID" type="s:QName"/>
</s:complexType>
Here's the new gSOAP generated header
class ns1__ItemValue : public xsd__anyType
{ public:
/// Element DiagnosticInfo of type xs:string.
char* DiagnosticInfo 0; ///< Optional element.
/// Element Value of type xs:anyType.
xsd__anyType* Value 0; ///< Optional element.
/// Element Quality of type "http://opcfoundation.org/webservices/XMLDA/1.0/":OPCQuality.
ns1__OPCQuality* Quality 0; ///< Optional element.
/// Attribute ValueTypeQualifier of type xs:QName.
@_QName ValueTypeQualifier 0; ///< Optional attribute.
/// Attribute ItemPath of type xs:string.
@char* ItemPath 0; ///< Optional attribute.
/// Attribute ItemName of type xs:string.
@char* ItemName 0; ///< Optional attribute.
/// Attribute ClientItemHandle of type xs:string.
@char* ClientItemHandle 0; ///< Optional attribute.
/// Attribute Timestamp of type xs:dateTime.
@time_t* Timestamp 0; ///< Optional attribute.
/// Attribute ResultID of type xs:QName.
@_QName ResultID 0; ///< Optional attribute.
};
Autres conseils
Here is something that might be of help. I cannot test it right now, so it is just a suggestion. Look at 'Void pointers': http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc11.9
struct myStruct
{
int __type; // the SOAP_TYPE pointed to by p
void *p;
};
As I understand from docs, this is used to serialize anything, with specifying it's type. So it might work to replace the _XML type of the Value element with this struct. Then you just have to set the __type attribute with the corresponding value:
struct ns1_Value {
int __type; // the SOAP_TYPE pointed to by p
void *p;
}
struct ns1_Value value;
int n;
value.p = &n;
value.__type = SOAP_TYPE_int;
Another way to solve the problem could be to serialize raw xml instead of the <Value> element... Or to think of some xs:element that extends xs:anyType, but defines a xs:type.. Actually gSoap is a cool product, but to get the best of it you need to learn how to hack it..