XML technologies / XML / Perl & XML / Programmer strategies / Perl and XML namespaces
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
We have seen namespaces since introducing them in Introduction to XML. Some applications, including XSLT and SOAP, require every element in a defined namespace. Their usefulness depends on how symbiotic the application is. A standalone application processing one document gains little; documents full of other applications’ fragments are different.
DocBook XML is not very symbiotic. An instance is usually a whole book or article. Allowed elements not explicitly assigned another namespace should appear in the official DocBook documentation. Yet embedding MathML elements like parasites among ordinary DocBook elements is common.
This helps for two reasons. First, DocBook can focus on technical authors’ needs without mathematical complexity overloading it. Its element <equation> mostly describes embedded graphics instead. MathML brings full mathematical functionality into DocBook without collisions because of its namespace. They work so well together that the DocBook DTD allows a MathML module. This plug-in defines a new element <mml:math> governed internally by the included MathML DTD. Validation alternates between DocBook and MathML DTDs.
Second, namespaces resemble embassies: within their scope, their country’s rules apply regardless of host country. They also resemble Perl namespaces separating variables, subroutines and symbols from Irgendein::Anderes::Package, unless defined in package main.
Namespaces signal another XML application invoked by the current one. If processing a vocabulary known to contain other namespaces, consider delegation to another program.
Suppose an XML file lists your household monkeys, mostly using your own elements plus some Monkey Markup Language elements. This standardized monkey-description language defines its own namespace:
<?xml version="1.0" encoding="ISO-8859-1">
<monkey-list>
<monkey>
<description xmlns:mm="http://www.jmac.org/projects/monkeys/mm/">
<mm:monkey> <!-- Beschreibung eines einzelnen Affen -->
<mm:name>Virtram</mm:name>
<mm:color>rötlich grau</mm:color>
<mm:favorite-foods>
<mm:food>Bananen</mm:food> <mm:food>Walnüsse</mm:food>
</mm:favorite-foods>
<mm:personality-matrix>
F6 30 00 0A 1B E7 9C 20
</mm:personality-matrix>
</mm:monkey>
</description>
<location>Wohnzimmer</location>
<job>Vogelscheuche</job>
</monkey>
<!-- Hier kommen die anderen Affenbeschreibungen -->
</monkey-list>URI identifiers
Many technologies, including XML namespaces, SAX2 and SOAP, use URIs as unique identifiers for features or properties. Meaning comes from agreement: standard or software authors define it, users rely on it. URIs often resemble URLs beginning with http://. Entering them in a browser often yields a disappointing HTTP 404. Their purpose is global uniqueness, not locating resources.
Developers commonly take a server or directory they control and append terms describing the URI’s purpose while ensuring local uniqueness. If you control "http://www.greenmonkey-markup.com/~jmac", then "http://www.greenmonkey-markup.com/~jmac/monkeyml/" is a good URI, whether a document exists or not: nobody else should create it. Polite developers do provide useful content, such as documentation, a DTD, an XML schema or similar material.
Another solution is a service such as PURL, unrelated to Perl, assigning a URI URL that redirects to other, possibly changing URLs.
URIs sometimes add information. Some processors carefully distinguish allowed URIs, reasonably so when version numbers are involved. A stylesheet processor distinguishes XSLT 1.0’s W3C URI from predecessor versions.
Robin Berjon’s module
XML::NamespaceSupporthelps with namespaces.
Fortunately we have the excellent module XML::MonkeyML to read MonkeyML into objects. It handles the special element personality-matrix, which condenses a monkey’s personality into a short hexadecimal code, at least in our imagination. We use it to predict reactions in a given situation:
#!/usr/bin/perl
# Das Programm erhält auf der Kommandozeile eine Affenaktion übergeben.
# Für jeden Affen in einer Liste (der Dateiname eines MonkeyML-Dokuments
# wird ebenfalls auf der Kommandozeile übergeben) wird eine Prognose der
# erwarteten Reaktion des Affen ausgegeben.
use warnings;
use strict;
use XML::LibXML;
use XML::MonkeyML;
my ($filename, $action) = @ARGV;
unless (defined ($filename) and defined ($action)) {
die "Usage: $0 <Datei-mit-Affenliste> <Aktion>\n";
}
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file($filename);
# Sammle die Elemente mit den Affenbeschreibungen
my @monkey_nodes = $parser->documentElement->findNodes("//monkey/description/mm:monkey");
foreach (@monkey_nodes) {
my $monkeyml = XML::MonkeyML->parse_string($_->toString);
my $name = $monkeyml->name . " the " . $monkeyml->color . "e monkey";
print "$name würde sich folgendermaßen verhalten:\n";
# Die Methode 'action' des MonkeyML-Objekts prognostiziert die Reaktion
print $monkeyml->action($action); print "\n";
}Here is the output:
$ ./money_action.pl monkeys.xml "Gib ihm eine Banane"
Virtram, der rötlich graue Affe, würde sich folgendermaßen verhalten:
Er nimmt die Banane, ißt sie und sagt "Ugh". Speaking of laziness, let us see what a helper module like XML::MonkeyML might look like.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Perl & XML” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top