From 6b15f5911aba773a20a9d34e4fb958360486e3cf Mon Sep 17 00:00:00 2001 From: ThomasG77 Date: Tue, 27 Dec 2011 11:05:29 -0800 Subject: [PATCH] Updated Managing complex map files using XML entities (markdown) --- ManagingLargeXmlFiles.md | 104 +++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 49 deletions(-) diff --git a/ManagingLargeXmlFiles.md b/ManagingLargeXmlFiles.md index 3b210dc..8ce538c 100644 --- a/ManagingLargeXmlFiles.md +++ b/ManagingLargeXmlFiles.md @@ -20,9 +20,11 @@ components using external entities and XInclude. ## Mapnik XML support Mapnik currently supports three different XML parsers: - - the boost spirit based parser - - the tinyxml parser - - libxml2 + +* the boost spirit based parser +* the tinyxml parser +* libxml2 + The three parsers differ in size, external dependencies and the number of XML features they support. The most comprehensive parser is the libxml2 parser and it is the only one that supports XML entities. As of Mapnik 0.6.0 libxml2 is the default @@ -34,9 +36,9 @@ when building the Mapnik source with SCons, and available in the Windows binarie If not default in your Mapnik version (< 0.6.0) the libxml2 parser is enabled by setting the XMLPARSER option at compile time: - - #!sh +```sh $ python scons/scons.py configure XMLPARSER=libxml2 +``` Of course this requires the libxml2 library and, depending on the distribution the corresponding devel package. If `xml2-config` is not in the PATH its location @@ -44,31 +46,31 @@ can be set using the `XML2_CONFIG` option. For example, if you have installed the latest libxml2 on mac os x via Macports, you might need to do: - - #!sh +```sh $ python scons/scons.py configure XML2_CONFIG=/opt/local/bin/xml2-config - +``` ## Internal Entities All XML parsers have some built-in entities to escape otherwise illegal characters: - - > - - < - - & - - " - - ' + +* > +* < +* & +* " +* ' The XML document type definition (DTD) provides a way to declare new, user defined entities: - - #!text/xml +```xml ]> +``` This XML document declares an internal entity named water_color. This entity is referenced by the bgcolor attribute of the Map element. The parser replaces all @@ -85,8 +87,7 @@ reoccurring value is a candidate for an entity. It is allowed to nest entities: - - #!text/xml +```xml @@ -100,17 +101,16 @@ It is allowed to nest entities: +``` However, these internal entities are not suitable for larger blocks. They also do not help with sharing common styles and layers between different maps. - ## External Entities External entities are declared by adding the keyword SYSTEM: - - #!text/xml +```xml @@ -127,6 +127,7 @@ External entities are declared by adding the keyword SYSTEM: +``` The entity declaration assigns the content of the file `settings/db_settings` to the entity `&db_settings;`. When parsed the reference to `&db_settings;` in the @@ -135,12 +136,13 @@ filename is given the file is searched relative to the document. The file `settings/db_settings` could look like this: - #!text/xml - postgis - www.example.org - 5433 - david - geo +```xml + postgis + www.example.org + 5433 + david + geo +``` Note that this is not a legal XML document on its own because it does not have a single root element. It is a list of elements. But the tags have to be well @@ -152,7 +154,7 @@ limited form of parameterization. Consider the following example: File earthquake.map: - #!text/xml +```xml @@ -173,23 +175,25 @@ File earthquake.map: &common_styles; &common_layers; +``` File earthquakes_since.lay: - #!text/xml - - earthquakes - - - - (SELECT * FROM earthquakes - WHERE year >= &since_year; ) AS earthquakes_since - - - &db_settings; - - - +```xml + + earthquakes + + + + (SELECT * FROM earthquakes + WHERE year >= &since_year; ) AS earthquakes_since + + + &db_settings; + + + +``` This is a quite flexible setup. It is very easy to add and remove thematic overlays. Other overlays may use the same parameters by referencing the @@ -198,7 +202,6 @@ same entities. Styles can be changed by replacing the reference to many map files all referencing the same set of styles and layer files but with different settings. - ## Entities Summary Entities provide a way to use symbolic names in the map file. This improves @@ -208,7 +211,6 @@ general more maintainable. External entities can store whole blocks of XML. This helps to build reusable collections of layers and styles. These reusable components can be parameterized using other entities as needed. - ## Including external files using XInclude libxml2 also provides support for decomposing large mapnik XML files through the use of XInclude. @@ -217,7 +219,7 @@ To enable XInclude in your root file, modify the Map container tag, adding the x File tests/data/good_maps/xinclude/map.xml: - #!text/xml +```xml @@ -229,6 +231,7 @@ File tests/data/good_maps/xinclude/map.xml: +``` Included files wrap their content within an Include tag. XInclude replaces the xi:include tags with the contents of the included file, yielding a single, merged XML document. mapnik's XML parser then merges the contents of the Include tag into the Map tag, resulting in an XML document tree identical to one produced by processing a single file containing @@ -236,7 +239,7 @@ all Style and Layer tags. File styles.xml: - #!text/xml +```xml @@ -254,10 +257,11 @@ File styles.xml: +``` File layers.xml: - #!text/xml +```xml @@ -270,6 +274,7 @@ File layers.xml: +``` ## Combining XInclude and External Entities @@ -279,7 +284,7 @@ A common pattern found in mapnik XML file sets is specifying the zoom levels for file: zoomsymbols.txt - #!text/html +```xml @@ -290,14 +295,14 @@ file: zoomsymbols.txt - +``` Adding a DOCTYPE line to the layers.xml file to include the external entities file, and adding parameterized minzoom and maxzoom attributes to the layer yields the file below: File layers_with_entities.xml: - #!text/xml +```xml @@ -312,6 +317,7 @@ File layers_with_entities.xml: +``` ## Further Reading