Archive for the ‘XML’ Category

Are you using a real XML parser

Monday, May 19th, 2008

Recently there’s been a bunch of announcements of new XML parsers that claim to be very fast, very small or both. I also see a lot of people get very enthusiastic about using them in their applications. Just the other day I got an email from a user asking if it was possible to use CodeSynthesis XSD with a light-weight XML parser that he found instead of Xerces-C++. Out of curiosity I checked the parser’s description and there I saw a number of common traits of most new, fast, and small XML parsers these days: no support for DTD (internal subset) and CDATA sections, limited support for character and entity references.

Once I tell people about these problems with their choice of XML parser, some say they don’t care since nobody uses these features anyway. It is true that most of these features are seldom used. You can get away with using a non-conforming XML parser if you control the production of XML you are planning to parse and thus restrict the set of XML constructs that can appear in your documents. This, however, is not a very common situation. If you control both the production and consumption of the data then you might as well choose a more natural (for your application and environment) and efficient (that’s why choose this new parser) exchange format than XML. A more common scenario is when you have to parse XML supplied by various third parties and once you say your format is XML then all bets are off; it is only a matter of time before someone sends you a perfectly valid XML document that your application won’t be able to handle.

The W3C XML specification defines two types of conforming XML parsers: validating and non-validating (see Section 5.1, “Validating and Non-Validating Processors”). Any parser that wants to be called capable of parsing XML 1.0 documents must at least satisfy the non-validating parser requirements. Besides the expected things like being able to parser elements and attributes as well as making sure they are well-formed, a conforming non-validating XML parser should also support the following:

  • At least UTF-8 and UTF-16 character encodings
  • CDATA sections (<![CDATA[<greeting>Hello, world!</greeting>]]>)
  • Character references (&#x20;)
  • Entity references including predefined (&amp;) and user-defined in the internal DTD subset
  • Parse and check for well-formedness the internal DTD subset
  • Normalize and supply default attribute values according to the internal DTD subset

The internal DTD subset consist of the DTD declarations that are found in the XML document itself as opposed to the external subset which consists of the declarations placed into separate DTD files and referenced from the XML documents. Here is a sample document that uses most of the above features (download it to test your favorite parser: test.xml):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE hello [
<!ENTITY greeting-text "hello">
<!ATTLIST greeting lang NMTOKEN "en">
<!ATTLIST name lang NMTOKEN "en">
]>
<hello>
  <greeting>&greeting-text;</greeting>
  <name lang="  fr  ">tout&#x20;le&#x20;<![CDATA[monde]]></name>
</hello>

Parsing this document with a conforming non-validating XML parser should be equivalent to parsing the following document:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<hello>
  <greeting lang="en">hello</greeting>
  <name lang="fr">tout le monde</name>
</hello>

Here is the list of C and C++ XML parsers that are either conforming or strive to be conforming:

  • Xerces-C++: C++, DOM, SAX2, validating (DTD and XML Schema)
  • Libxml2: C, DOM, SAX2, Pull, validating (DTD)
  • Expat: C, SAX2, non-validating, small & fast
  • Faxpp: C, Pull, non-validating (no default/normalized attributes), small & fast

And here is a list of parsers that while calling themselves XML parsers, are actually parsers for markup languages that are subsets of XML (based on their description at the time of writing):

  • VTD-XML
  • Rapidxml
  • TinyXML
  • XML parser provided by applied-mathematics.net

Xerces-C++ 3.0.0 beta 1 released

Friday, March 14th, 2008

I’ve spent the past three weeks prepping the Xerces-C++ 3.0.0 code for the upcoming release which culminated in the publishing of the first beta yesterday. The major change in 3.0.0 compared to the 2-series releases is the new, autotools-based build system for Linux/UNIX platforms. Other improvements in 3.0.0 include:

  • Project files for VC 9
  • Support for the ICU transcoder in VC 7.1, 8, and 9 project files
  • libcurl-based net accessor
  • Support for XInclude
  • Support for a subset of XPath
  • Conformance to the final DOM Level 3 interface specification
  • Ability to provide custom DOM memory manager
  • Better 64-bit support
  • Cleaned up error messages
  • Better tested, including against W3C XML Schema test suite
  • Removal of the deprecated code

My primary goals in this release are to make it cleaner, easier to build, better tested, as well as to provide better XML Schema support. And it does feel that the 3.0.0 codebase is on track to achieve these goals. If you are planning to upgrade to 3.0.0 once the final version is out, I suggest that you give this beta a try and report any problems so that they can be fixed before the final release. For more details on this beta see the official announcement.

CodeSynthesis XSD 3.1.0 released

Wednesday, February 13th, 2008

XSD 3.1.0 was released a couple of days ago. For an exhaustive list of new features see the official announcement. In this post I would like to go into more detail on a few major features, namely the file-per-type compilation mode and configurable identifier naming conventions.

File-per-type compilation mode

First, some background on the kinds of problems this feature is meant to solve. While in most cases it is natural to generate one set of source files from each schema file and map XML Schema include and import constructs to the preprocessor #include directives, XML Schema include and import mechanisms are quite a bit less strict compared to #include. For example, you can have two schemas each with a type that inherits from a base in another schema (that is, these schemas are dependent on each other and this dependency involves inheritance). Or, you can have a schema that does not include/import definitions for all the types it is referencing. Instead such a schema relies on being included or imported into another schema which provides the missing definitions (while this can also happen in C++, it is not very common). As a result, sometimes it is not possible to compile the schemas separately and/or map XML Scheme include/import to C++ #include. For such situations the file-per-type compilation mode was introduced in addition to the existing file-per-schema mode.

In the new mode (the --file-per-type command option), the XSD compiler generates a separate set of files for each type defined in XML Schema. It still generates a set of source files corresponding to the schema files which now include the header files for the types and contain parsing and serialization functions. In this compilation mode you only need to compile the root schema for your vocabulary; the code will be automatically generated for all included and imported schemas. If your vocabulary has several root schemas which in turn include or import a common subset of schemas then you will need to specify all these root schemas in a single invocation of the compiler.

One reason why the file-per-schema mode should be preferred whenever possible is the potentially large number of source files that are generated in the file-per-type mode (some of the schemas that we have tested contain 1000-1,500 types). To minimize the impact of the file-per-type mode on the C++ compilation time, it is a good idea to generate the XML Schema namespace into a separate header file (see the --generate-xml-schema and --extern-xml-schema options) and to set up a precompiled header.

To help dealing with a potentially large number of files that the new mode produces, the new --file-list option was added to the XSD compiler that allows you to write a list of generated source files into a file. The --file-list-prologue, --file-list-epilogue, and --file-list-delim options allow you to turn this file into, for example, a makefile fragment with the list of files assigned to a variable. The following GNU make fragment shows how to put all of the above information together:

XSD    := ... # path to the XSD compiler
LIBXSD := ... # path to the XSD runtime library
 
driver:
 
# Schema compilation.
#
xsd      := ... # list of all schema files
xsd_root := ... # root schema(s)
 
-include gen.make
 
gen.make: $(xsd)
  $(XSD) cxx-tree --file-per-type --output-dir gen 
--file-list $@ --file-list-prologue "gen := " --file-list-delim " \\n" 
--extern-xml-schema xml-schema.xsd --cxx-prologue '#include "all.hxx"' 
$(xsd_root)
 
gen/xml-schema.hxx:
  $(XSD) cxx-tree --generate-xml-schema --output-dir gen xml-schema.xsd
 
src := driver.cxx $(filter %.cxx,$(gen))
obj := $(src:.cxx=.o)
 
# Precompiled header.
#
$(obj): gen/all.hxx.gch
 
gen/all.hxx.gch: gen/all.hxx gen/xml-schema.hxx
  $(CXX) -I$(LIBXSD) -o $@ $<
 
# Object code and driver.
#
driver: $(obj) -lxerces-c
  $(CXX) -o $@ $^
 
%.o: %.cxx
  $(CXX) -I$(LIBXSD) -c $< -o $@

The gen/all.hxx file is the precompiled header for the project and could look like this:

#ifndef GEN_ALL_HXX
#define GEN_ALL_HXX
 
#warning precompiled header is not used
 
#include "xml-schema.hxx"
 
#endif // GEN_ALL_HXX

Another interesting aspect of the file-per-type compilation mode is how it is implemented in XSD. A straightforward but complex approach would have been to support this mode in the code generators in addition to the file-per-schema mode. Instead, an internal schema graph transformation was implemented that transforms the semantic graph to make it appear as if each type is in a separate schema file. After this transformation the unchanged code generators are used as in the file-per-schema mode.

Configurable identifier naming conventions

One common objection to using automatic code generation is the difference between the identifier naming conventions used in a project and in the generated code. To address this concern, the XSD compiler allows you to specify a naming convention that should be used in the generated code for the C++/Tree mapping.

The two new options, --type-naming and --function-naming, allow you to select type and function naming conventions from a predefined set of widely-used styles. You can also provide regular expressions to customize or completely override one of the predefined styles.

Available type naming conventions are K&R (for example, test_type), upper-camel-case (for example, TestType), and Java (the same as upper-camel-case). Available function naming conventions are K&R (for example, test_function), lower-camel-case (for example, testFunction), and Java (for example, getTestFunction for accessors and setTestFunction for modifiers).

For more information see the NAMING CONVENTION section in the XSD Compiler Command Line Manual (man pages).