XML technologies / XML / Perl & XML / SAX / DTD handlers
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
XML::Parser::PerlSAX supports another group of handlers for DTD events. They handle everything before the root element, including the XML declaration, the document type declaration, internal subset, and so on, collectively called the document prolog. To reproduce a document completely and faithfully, as in a filter, implement some of these handlers to preserve the prolog. This was missing from the previous example.
These handlers have other uses, such as preloading entity definitions for special handling, though normally the parser does this. The following table lists the DTD handlers.
Table: PerlSAX DTD handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The handler entity_decl( ) handles all entity declarations unless a more specific handler exists. An unparsed entity declaration calls the handler entity_decl( ), unless the handler unparsed_entity_decl( ) is defined and takes precedence.
Parameters of an entity_decl( ) handler depend on entity type. For internal entities the Value property is set; otherwise it is absent. PublicId and SystemId locate an external entity’s content file, but do not apply to internal entities. The base (Base) is a URL resolving a relative SystemId.
Notation declarations assign a special type identifier to an entity, for example “date”, indicating that it contains a date. Notations are rarely used in practice, so we can safely omit them here.
The property Model of the handler element_decl( ) contains the element grammar, called its content model, describing what the DTD permits inside it.
An attribute-list declaration may declare several attributes. The parser handles this, passing the handler attlist_decl( ) one attribute at a time.
The optional document type declaration follows the XML declaration. Its parameter Name is the root element’s name. PublicId and SystemId tell the processor where to load an external DTD. The parameter Internal contains the entire internal subset as a string if individual entity and element declarations are to be skipped.
To extend our filter to pass the prolog through unchanged, use the following handlers.
Example: An improved filter
# Behandlung von XML-Deklarationen
#
sub xml_decl {
my( $self, $properties ) = @_;
output( "<?xml version=\"" . $properties->{'Version'} . "\"" );
my $encoding = $properties->{'Encoding'};
output( " encoding=\"$encoding\"" ) if( $encoding );
my $standalone = $properties->{'Standalone'};
output( " standalone=\"$standalone\"" ) if( $standalone );
output( "?>\n" );
}
#
# Behandlung von Dokumenttypdeklarationen:
# Duplikation des Originals, so gut wie möglich
#
sub doctype_decl {
my( $self, $properties ) = @_;
output( "\n<!DOCTYPE " . $properties->{'Name'} . "\n" );
my $pubid = $properties->{'PublicId'};
if( $pubid ) {
output( " PUBLIC \"$pubid\"\n" );
output( " \"" . $properties->{'SystemId'} . "\"\n" );
} else {
output( " SYSTEM \"" . $properties->{'SystemId'} . "\"\n" );
}
my $intset = $properties->{'Internal'};
if( $intset ) {
$in_intset = 1;
output( "[\n" );
} else {
output( ">\n" );
}
}
#
# Behandlung von Entitydeklarationen im internen Bereich:
# Stelle die ursprüngliche Deklaration wieder her
#
sub entity_decl {
my( $self, $properties ) = @_;
my $name = $properties->{'Name'};
output( "<!ENTITY $name " );
my $pubid = $properties->{'PublicId'};
my $sysid = $properties->{'SystemId'};
if( $pubid ) {
output( "PUBLIC \"$pubid\" \"$sysid\"" );
} elsif( $sysid ) {
output( "SYSTEM \"$sysid\"" );
} else {
output( "\"" . $properties->{'Value'} . "\"" );
}
output( ">\n" );
}
The next example shows the improved filter’s output.
Example: Improved filter output
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE book
SYSTEM "/usr/local/prod/sgml/db.dtd"
[
<!ENTITY schwachsinn "hoo hah blah blah">
]>
<book id="mybook">
<title>Einführung in GRXL</title>
<chapter id="intro">
<title>Was ist GRXL?</title>
<comment> Der Titel sollte schmissiger werden. </comment>
<para>
Einfach ein weiteres Akronym. Das war unsere ursprüngliche Absicht, aber dann stellte
sich mehr und mehr heraus, wie verblüffend nützlich diese neue Technologie namens
<literal>GRXL</literal> ist. Betrachten Sie das folgende Programm:
</para>
<programlisting>AH aof -- %%%%
{{{{{{ let x = 0 }}}}}}
print! <lineannotation>wow</lineannotation>
oder nicht!</programlisting>
<comment> Welchen Font sollten wir hier benutzen? </comment>
<para>
Was das Programm macht? Wen kümmert’s? Es sieht einfach cool aus, oder nicht?
Wirklich, ich kann echt nur eines sagen: "&schwachsinn;".
</para>
</chapter>
</book>Much better: a complete filter. The main handler processes elements and content; DTD handlers process what lies outside the root.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are 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