1This example shows how to customize parsing and serialization code for the 2xsd:double XML Schema built-in type using the type customization mechanism 3provided by the C++/Tree Mapping. For more information on type customization 4see the C++/Tree Mapping Customization Guide, particularly sections 1 and 4: 5 6http://wiki.codesynthesis.com/Tree/Customization_guide 7 8In this example our schema uses xsd:double to represent a price. There are 9two potential problems with this choice of a price type. First, xsd:double 10can be serialized in the scientific notation which would be an unusual way 11of representing a price. Second, we would like to limit the number of 12fraction digits in our prices to 2. Furthermore, we would like to always 13have two fraction digits, even if one or both of them are zeros, for 14example: 12.99, 12.90, 12.00. 15 16In case we can modify the schema, a better approach would be to define the 17price type as a restriction of the xsd:decimal type (always fixed notation) 18and specify the fractionDigits facet to limit the number of fraction digits 19to 2. However, there is no way in XML Schema to specify that there should 20always be exactly 2 fraction digits. Therefore, it may still be desirable 21to customize this price type to get the required serialization behavior. 22 23Finally, it is worth noting that the behavior achieved in this example via 24type customization can also be achieved by compiling your code with the 25following macros defined: 26 27XSD_TREE_DOUBLE_FIXED 28XSD_TREE_DOUBLE_PRECISION 2 29 30However, the type customization approach while requiring more work is 31cleaner since it does not rely on global macro definitions. 32 33This example consists of the following files: 34 35order.xsd 36 XML Schema definition for a simple order vocabulary. 37 38double-custom.hxx 39double-custom.cxx 40 Custom parsing and serialization code for the xsd:double types. The 41 double-custom.hxx file is included at the end of the xml-schema.hxx 42 file described below. 43 44xml-schema.hxx 45 C++ types for XML Schema built-in types. This header file is generated 46 by the XSD compiler using the --generate-xml-schema option. The 47 --custom-type option is used to customize the xsd:double type. The 48 --hxx-epilogue option is used to include the double-custom.hxx file 49 at the end of this file. 50 51order.hxx 52order.cxx 53 C++ types generated from order.xsd. The --extern-xml-schema option 54 is used to include xml-schema.hxx into order.hxx. 55 56driver.cxx 57 Test driver for the example. It creates a sample order and then 58 writes it to XML to test the custom xsd:double serialization code. 59 60To run the example simply execute: 61 62$ ./driver 63