[xsd-users] Fixed Attributes

George Vassilakes george at sbdev.net
Fri Oct 17 04:38:06 EDT 2008


Hi Boris,

The problem is that when I create a new instance of a class generated by 
XSD for a type it does not initialise the member attributes with the 
default/fixed values. So if I then serialize this instance it only 
writes the attributes that are present to the XML document. If I then 
parse this document it creates instances of these types that do include 
these attributes.
So basically when it desirializes an XML document the instances include 
the default/fixed attributes, however when creating a new instance in 
C++ then these attributes are not set. This is a problem with fixed 
attributes defined on the supertype

consider the following schema

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <xsd:complexType name="ExampleBase" abstract="true">
   <xsd:attribute name="attributeFixed" type="xsd:string"/>
 </xsd:complexType>
 <xsd:element name="ExampleBase" type="ExampleBase"/>


 <xsd:complexType name="ExampleRestr" abstract="true">
   <xsd:complexContent>
     <xsd:restriction base="ExampleBase">
       <xsd:attribute name="attributeFixed" type="xsd:string" 
fixed="TEST"/>
     </xsd:restriction>
   </xsd:complexContent>
 </xsd:complexType>
 <xsd:element name="ExampleRestr" type="ExampleRestr" 
substitutionGroup="ExampleBase"/>


 <!-- Encrypt disk command take as a parameter the disk details-->
 <xsd:complexType name="Example">
   <xsd:complexContent>
     <xsd:extension base="ExampleRestr">
       <xsd:attribute name="attribute1" type="xsd:string" fixed="fixed2"/>
       <xsd:attribute name="attribute2" type="xsd:string"/>
       <xsd:attribute name="attribute3" type="xsd:string"/>
     </xsd:extension>
   </xsd:complexContent>
 </xsd:complexType>
 <xsd:element name="Example" type="Example" 
substitutionGroup="ExampleBase"/>

</xsd:schema>


Then run the following code


#include "Example.hxx"
#include <string>
#include <iostream>

int main(int argc, char* argv[])
{
   Example ex;
   ex.attribute2("att2");
   ex.attribute3("att3");

   // now print the details it will show that the attribute fixed is not 
present
   std::cout << ex.attributeFixed() << std::endl;
   std::cout << ex.attribute1() << std::endl;
   std::cout << ex.attribute2() << std::endl;
   std::cout << ex.attribute3() << std::endl;

   // serialize
   xml_schema::namespace_infomap map;
   map[""].name = "";
   map[""].schema = "Example.xsd";
   std::ostringstream stream;
   Example_(stream, ex, map);

   // read serialized class and print again
   std::istringstream is(stream.str());
   std::auto_ptr<::Example> ex2 = Example_(is);

   // now print the details this time attributeFixed is present
   std::cout << ex2->attributeFixed() << std::endl;
   std::cout << ex2->attribute1() << std::endl;
   std::cout << ex2->attribute2() << std::endl;
   std::cout << ex2->attribute3() << std::endl;

   system("PAUSE");
   return 0;
}

Regards
George


Boris Kolpackov wrote:
> Hi George,
>
> George Vassilakes <george at sbdev.net> writes:
>
>   
>> Is it possible to get the XSD generated classes when serializing to 
>> XML to also include the fixed value attributes?
>>     
>
> This is the default behavior. All default and fixed attributes are
> present in the resulting XML unless you specified the
> --omit-default-attributes option.
>
>
>   
>> If I create instances of the DT_EncryptDiskCmd class and serialise them 
>> and load the using the DTMessage.xsd then the serviceName is 
>> automatically set by the fixed value.
>> However if I parse the XML using the Message.xsd the value is not set as 
>> this was not output by the serialization.
>>     
>
> I am having a hard time understanding what you mean by "parse the XML 
> using the Message.xsd". Can you provide a small but compilable example
> (i.e., a schema, sample XML file, and a test driver) that demonstrates
> the problem you are observing?
>
> Boris
>
>
>   





More information about the xsd-users mailing list