[xsd-users] Fixed Attributes

Boris Kolpackov boris at codesynthesis.com
Fri Oct 17 06:23:08 EDT 2008


Hi George,

George Vassilakes <george at sbdev.net> writes:

> 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>

Thanks for the example, now I know what's going on. It is quite hard
to provide a mapping for complex type restriction in C++ (it quickly
becomes a huge bag of special cases). So what we did in the C++/Tree
mapping is to ignore it (I am simplifying it a bit here). In your
case the ExampleRestr class simply inherits from ExampleBase. In
most cases it works just fine but sometimes a useful feature like
the above gets ignored as well. The reason why you get the default
attribute set when you create the object model from XML is because
XML Schema validator in Xerces-C++ "expands" default/fixed attributes.

The only solution I can offer at the moment is to "touch up" the
ExampleRestr class with type customization and set the default
attribute value manually (from your earlier email I gather that
you don't have too many of these "intermediate" types).

For more information on type customization, see the C++/Tree Mapping
Customization Guide:

http://wiki.codesynthesis.com/Tree/Customization_guide

There is also a bunch of examples in the examples/cxx/tree/custom/
directory with the ones relevant to your case being contacts and
taxonomy (they are also discussed in the guide). In your case all
you will need to do is set the default attribute value in the
default constructor:

class ExampleRestr: public ExampleRestr_base
{
  ExampleRestr ()
  {
    attributeFixed ("TEST");
  }

  // Other c-tors simply forward to ExampleRestr_base
  //
  ...

};

Boris




More information about the xsd-users mailing list