summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/custom/double/README
blob: bf951044bb713014d56a760b7babbc6bcefff126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
This example shows how to customize parsing and serialization code for the
xsd:double XML Schema built-in type using the type customization mechanism
provided by the C++/Tree Mapping. For more information on type customization
see the C++/Tree Mapping Customization Guide, particularly sections 1 and 4:

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

In this example our schema uses xsd:double to represent a price. There are
two potential problems with this choice of a price type. First, xsd:double
can be serialized in the scientific notation which would be an unusual way
of representing a price. Second, we would like to limit the number of
fraction digits in our prices to 2. Furthermore, we would like to always
have two fraction digits, even if one or both of them are zeros, for
example: 12.99, 12.90, 12.00.

In case we can modify the schema, a better approach would be to define the
price type as a restriction of the xsd:decimal type (always fixed notation)
and specify the fractionDigits facet to limit the number of fraction digits
to 2. However, there is no way in XML Schema to specify that there should
always be exactly 2 fraction digits. Therefore, it may still be desirable
to customize this price type to get the required serialization behavior.

Finally, it is worth noting that the behavior achieved in this example via
type customization can also be achieved by compiling your code with the
following macros defined:

XSD_TREE_DOUBLE_FIXED
XSD_TREE_DOUBLE_PRECISION 2

However, the type customization approach while requiring more work is
cleaner since it does not rely on global macro definitions.

This example consists of the following files:

order.xsd
  XML Schema definition for a simple order vocabulary.

double-custom.hxx
double-custom.cxx
  Custom parsing and serialization code for the xsd:double types. The
  double-custom.hxx file is included at the end of the xml-schema.hxx
  file described below.

xml-schema.hxx
  C++ types for XML Schema built-in types.

  This header file is generated by the XSD compiler in the
  --generate-xml-schema mode using the following command line:

  xsd cxx-tree --generate-xml-schema --generate-serialization \
      --custom-type double=double \
      --hxx-epilogue '#include "double-custom.hxx"' xml-schema.xsd

  The --custom-type option is used to customize the xsd:double type. The
  --hxx-epilogue option is used to include the double-custom.hxx file at
  the end of this file.

order.hxx
order.cxx
  C++ types are generated by the XSD compiler from order.xsd using the
  following command line:

  xsd cxx-tree --generate-serialization --extern-xml-schema xml-schema.xsd \
      order.xsd

  The --extern-xml-schema option is used to include xml-schema.hxx into
  order.hxx.

driver.cxx
  Test driver for the example. It creates a sample order and then
  writes it to XML to test the custom xsd:double serialization code.

To compile and link the example manually from the command line we can use
the following commands (replace 'c++' with your C++ compiler name):

c++ -DXSD_CXX11 -c order.cxx
c++ -DXSD_CXX11 -c double-custom.cxx
c++ -DXSD_CXX11 -c driver.cxx
c++ -o driver driver.o order.o double-custom.o -lxerces-c

Note that we need to define the XSD_CXX11 preprocessor macro since the
source code includes libxsd headers directly.

To run the example execute:

./driver