[xsde-users] hex integer representation

Boris Kolpackov boris at codesynthesis.com
Wed Oct 7 07:25:25 EDT 2009


Hi Duncan,

Duncan.Perrett at elekta.com <Duncan.Perrett at elekta.com> writes:

> I have an ID in the XML file which is a hex number, eg: 
> 
>         <ID>0x3C</ID>
> 
> my schema decribes this as 
> 
>         <xsd:element name="ID" type="xsd:string"/>
> 
> BUT this isn't a string. 
> 
> Can I use decimal even though it's in hex?

No, none of the built-in schema types support the hex notation you
are using above.

> Should I try and get the XML to use pure decimal?

That would probably be the easiest way. If you need to to keep it
in hex and also want to a convenient object model, then you can
use type/parse/serializer customization to re-map it to, say, int.
You would basically need to do the following:

1. Define a type in XML Schema, say hexInt:

<xsd:simpleType name="hexInt">
  <xsd:restriction base="xsd:string"/>
</xsd:simpleType>

<xsd:element name="ID" type="hexInt"/>

2. When compiling your schemas, re-map hexInt to, say, int. For more
   information on how to do this see Section 4.8, "Customizing the 
   Object Model" in the C++/Hybrid Mapping Getting Started Guide:

   http://www.codesynthesis.com/projects/xsde/documentation/cxx/hybrid/guide/#4.8

3. Provide custom parser and serializer implementations for the hexInt
   type. You can use, for example strtoul or std::istringstream to convert
   the string to int and sptrintf or std::ostringstream to convert it
   back to string. For more information on this part, see Section 6.1,
   "Customizing Parsers and Serializers":

   http://www.codesynthesis.com/projects/xsde/documentation/cxx/hybrid/guide/#6.1

There is also an example in custom/wildcard which shows how to customization
a type in the object model as well as the parser and serializer for it.

Boris



More information about the xsde-users mailing list